How to add External javascript library in Ionic2 Application

@rhuldip For these kind of files, in my case I’ve put them inside /src/assets/js/, because they will be copied to the /www/assets/js/ folder, then I reference it in index.html and declare it (you can declare in declarations.d.ts).

For example, lets say you have a file named my-library.min.js that is accessed through the MyLibrary global variable (that is, window.MyLibrary). Then:

1) Put my-library.min.js inside /src/assets/js/ (if the folder /src/assets/ doesn’t have a sub-folder /js, just create it).

2) In /src/index.html include the script tag:

<script src="assets/js/my-library.min.js"></script>

3) In /src/declarations.d.ts include:

declare var MyLibrary: any;

4) Use it:

export class MyApp implements OnInit {

	public ngOnInit() {
		console.log('MyLibrary', MyLibrary);
		MyLibrary.doSomething();
	}
	
}
3 Likes