Adding jquery library in angular-cli project - Install jquery via npm :
Install typings for the library: To add typings for a library, do the following:
Add jquery to angular-cli-build.js file to vendorNpmFiles array:
This is required so the build system will pick up the file. After setup the angular-cli-build.js should look like this:
Browse the node_modules and look for files and folders you want to add to the vendor folder.
Configure SystemJS mappings to know where to look for jquery :
SystemJS configuration is located in system-config.ts and after the custom configuration is done the related section should look like:
- In your src/index.html add this line
Your other options are: or and
Importing and using jquery library in your project source files:
Import jquery library in your source .ts files like this:
If you followed the steps correctly you should now have jquery library working in your project. Enjoy! |
| Add 3rd party library that does not have typings Notice, this is only for angular-cli up to 1.0.0-beta.10 version ! Some libraries or plugins may not have typings. Without these, TypeScript can't type check them and therefore causes compilation errors. These libraries can still be used but differently than imported modules.
Include a script reference to the library on your page (`index.html`)
1. These scripts should add a global (eg. `THREE`, `mapbox`, `$`, etc.) or attach to a global
In the component that requires these, use `declare` to initialize a variable matching the global name used by the lib. This lets TypeScript know that it has already been initialized. [1](http://stackoverflow.com/a/13252853/2554793)
Some libs attach to window, which would need to be extended in order to be accessible in the app.
Use the lib in your components as needed.
1. NOTE: Some libs may interact with the DOM and should be used in the appropriate [component lifecycle](https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html) method.
It is possible to install other libraries following, this approach, however, there might be a need to specify the module type, main file, and default extension. |