简体   繁体   中英

How to add a custom JS file into a angular project?

I am working on an angular project with my friends for our class project and we want a custom Js file with certain methods to be added to our project but adding this to the app.component.html file inside didn't work. How to add the files to the project?

you can do this in two ways:

If you want the custom js to be global, add the path to scripts array in angular-cli.json file

"scripts": [
    "../custom" 
 ];

Otherwise if you want the custom js to be in specific component,

import * as custom from './custom';
custom.method();

firsfile and secondfile are the member variable of typescript class

Initalise this member var in constructor like this.
this.firstFile= require('./firstfile'); // firstfile.js
this.secondFile= require('./secondfile'); //secondfile.js

now use same as you do in JavaScript after importing module files.

Hope it will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM