How can I import and use GSAP in an Ionic/Angular project?

可紊 提交于 2019-12-13 05:14:43

问题


I have a fresh Ionic project ('blank' template). I have run npm install gsap --save.

This is my app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

import { TweenMax } from "gsap/all";

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    TweenMax,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

And this is my home.ts:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { TweenMax } from "gsap/all";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(
    public navCtrl: NavController,
    public TweenMax: TweenMax
  ) {

  }

  ionViewDidLoad(){
    TweenMax.from(".logo", .5, { 
      opacity: 0,
      y: -72
    });
  }

}

But my app is erroring with:

Runtime error: Can't resolve all parameters for TweenMax: (?, ?, ?).

What's going on here and why am I getting this error? I have tried all of the import types from the npm page and get the same result every time.


回答1:


GSAP TweenMax doesn't provide definition files by default.

npm install --save-dev @types/greensock

Component:

declare var TweenMax: any;

Reference : https://medium.com/@mr.frag85/using-gsap-with-angular-6-project-it-works-on-prod-too-9ac036f21487

Sample Stackblitz : https://stackblitz.com/edit/angular-tkmxb4?file=app%2Fapp.component.ts

For Ionic: please refer GSAP in Ionic project



来源:https://stackoverflow.com/questions/53024864/how-can-i-import-and-use-gsap-in-an-ionic-angular-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!