Installing 3rd party plugins with angular-cli@1.0.0-beta.10
Adding jquery library in angular-cli project
Section titled “Adding jquery library in angular-cli project”- Install jquery via npm :
npm install jquery --saveInstall typings for the library:
To add typings for a library, do the following:
typings install jquery --global --saveBrowse the node_modules and look for files and folders you want to add to the vendor folder.
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
module.exports = function(defaults) { return new Angular2App(defaults, { vendorNpmFiles: [ // ... 'jquery/dist/*.js'
] });};/** Map relative paths to URLs. */const map: any = { 'jquery': 'vendor/jquery'};
/** User packages configuration. */const packages: any = {
// no need to add anything here for jquery
};- In your src/index.html add this line
<script src="vendor/jquery/dist/jquery.min.js" type="text/javascript"></script>Your other options are:
<script src="vendor/jquery/dist/jquery.js" type="text/javascript"></script>or
<script src="/vendor/jquery/dist/jquery.slim.js" type="text/javascript"></script>and
<script src="/vendor/jquery/dist/jquery.slim.min.js" type="text/javascript"></script>declare var $:any;
@Component({})export class YourComponent { ngOnInit() { $.("button").click(function(){ // now you can DO, what ever you want }); console.log(); }}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
Section titled “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.
<script src="//cdn.somewhe.re/lib.min.js" type="text/javascript"></script><script src="/local/path/to/lib.min.js" type="text/javascript"></script>-
1. These scripts should add a global (eg. `THREE`, `mapbox`, `$`, etc.) or attach to a global
declare var <globalname>: any;Some libs attach to window, which would need to be extended in order to be accessible in the app.
interface WindowIntercom extends Window { Intercom: any; }declare var window: WindowIntercom;@Component { ... }export class AppComponent implements AfterViewInit { ... ngAfterViewInit() { var geometry = new THREE.BoxGeometry( 1, 1, 1 ); window.Intercom('boot', { ... } }}-
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.
- NOTE: Some libs may interact with the DOM and should be used in the appropriate component lifecycle method.
Remarks
Section titled “Remarks”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.
'lodash': { format: 'cjs', defaultExtension: 'js', main: 'index.js' } 'moment': { main: 'moment.js' }