In my angular cli project, I need to refer external js library which are not node packages. So, they will not be under node_modules folder as they are not installed by npm. These are loose js files.
Below link explains how do we refer them in scripts array of .angular-cli.json file. However, it doesn't tell about where should external js files be added in folder structure of angular cli project if they are not node modules. Angular Cli Webpack, How to add or bundle external js files?
You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.
Step 1: Create a js named directory inside the assets directory and put the JavaScript (nand. js) file inside it. Step 2: Open the index. html and add a script tag to add the JavaScript file.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
Add your external js file inside the assets folder like : assets=>js=>filename.js
.angular-cli.json
"scripts": [
"../src/assets/js/externalfilename.js"
]
important after changes in .angular-cli.json file. Stop the project then start
How its use in ts file example below :
editor.js
<!-- assets/js/editor.js -->
Editor = function() {
//This is my javascript function
console.log('Editor');
}
editor.js will add in .angular-cli.json
"scripts": [
"../src/assets/js/editor.js"
]
in component.ts file
declare var Editor: any;
export class EditorComponent {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With