17.10.2018 Views

Angular

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 18: Installing 3rd party plugins<br />

with angular-cli@1.0.0-beta.10<br />

Section 18.1: Add 3rd party library that does not have typings<br />

Notice, this is only for angular-cli up to 1.0.0-beta.10 version !<br />

Some libraries or plugins may not have typings. Without these, TypeScript can't type check them and therefore<br />

causes compilation errors. These libraries can still be used but differently than imported modules.<br />

1.<br />

Include a script reference to the library on your page (index.html)<br />

<br />

<br />

These scripts should add a global (eg. THREE, mapbox, $, etc.) or attach to a global<br />

2.<br />

In the component that requires these, use declare to initialize a variable matching the global name used by<br />

the lib. This lets TypeScript know that it has already been initialized. 1<br />

declare var : any;<br />

Some libs attach to window, which would need to be extended in order to be accessible in the app.<br />

interface WindowIntercom extends Window { Intercom: any; }<br />

declare var window: WindowIntercom;<br />

3.<br />

Use the lib in your components as needed.<br />

@Component { ... }<br />

export class AppComponent implements AfterViewInit {<br />

...<br />

ngAfterViewInit() {<br />

var geometry = new THREE.BoxGeometry( 1, 1, 1 );<br />

window.Intercom('boot', { ... }<br />

}<br />

}<br />

NOTE: Some libs may interact with the DOM and should be used in the appropriate component<br />

lifecycle method.<br />

Section 18.2: Adding jquery library in angular-cli project<br />

1.<br />

Install jquery via npm :<br />

npm install jquery --save<br />

Install typings for the library:<br />

To add typings for a library, do the following:<br />

GoalKicker.com – <strong>Angular</strong> 2 Notes for Professionals 87

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!