Indeed, the answer is mostly yes, with a caveat depending on the use of libraries. Angular-cli may encounter some difficulties when integrating third-party libraries. While awaiting a proper fix, I can provide a workaround. Suppose you wish to utilize _
from lodash
in your code. Here is a working example:
In order to install lodash:
npm install lodash --save
typings install lodash --ambient --save
Prior to that, ensure typings are installed globally:
npm install typings -g
Next, in your angular-cli-build.json file, maintain the following structure:
module.exports = function(defaults) {
return new Angular2App(defaults, {
vendorNpmFiles: [
...
'lodash/**/*.js'
]
});
};
Furthermore, in your system-config.ts file:
/** Map relative paths to URLs. */
const map: any = {
'lodash': 'vendor/lodash/lodash.js'
};
/** User packages configuration. */
const packages: any = {
'lodash': {
format: 'cjs'
}
};
In your src/index.html, add this line (a peculiar fix):
<script src="/vendor/lodash/lodash.js" type="text/javascript"></script>
Now, in the component where you intend to use lodash, follow this approach:
declare var _:any;
@Component({
})
export class YourComponent {
ngOnInit() {
console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
}
}
This method has proven successful for me :). I have incorporated numerous third-party libraries into my angular2 project using angular-cli. Hopefully, it will also benefit you.
Edit:
If npm is unavailable for your third-party libraries, create an assets
folder within your src
directory. Inside, include separate folders for js
, css
, and images
. Place your third-party js files in the js
folder. Then reference the js file in your index.html
as follows:
<script src="assets/js/your_js.js"></script>
Upon running ng build
or ng serve
, the public
folder will be automatically updated with your assets/js
. I hope this clarifies the entire process :)