Integrating an external package like gsap with angular 2 follows a similar process to adding any other package, such as jquery or firebase.
However, it is important to note that currently there are no typings files available for the gsap module. This means you cannot selectively import certain parts of the package as desired, leading to a lack of intellisense support in typescript. Despite this limitation, you can still utilize gsap by following these steps:
Step 1: Install it using npm
npm install gsap --save
Step 2: Address typescript errors related to missing classes by adding the following line to your typings.d.ts
file:
declare var module: { id: string };
declare let TimelineMax: any; // Declare it as any to satisfy the Typescript compiler.
// No more error complaints, Typescript is happy now:)
If typings files are created for the gsap module, move on to Step 4:
typings install gsap --save. Then include: /// <reference path="../typings/path to your gsap typings" />
Step 3: Utilize gsap in your component - e.g., in app.component.ts
:
import 'gsap' // Necessary for TimelineMax() functionality
// Decorator goes here.
export class AppComponent implements OnInit{
ngOnInit(){
let targetObject = document.getElementByTagName('h1');
let tl = TimelineMax(); // Errors resolved
tl.from(targetObject, 1, { x:400 });
tl.play();
}
}
Step 4: With this setup, there is no need to modify the main.ts (bootstrap file). Enjoy incorporating gsap into your project!