简体   繁体   中英

How to import .ts into another .ts file in Angular 5 app?

I have some utils methods inside a utils.ts file which need to be reused by several components. I'm not sure whether it possible and if yes where to put the import and what is the syntax if any. I've tried the index.html file:

<script src="assets/ts/utils.js?v=1.2"></script>   

And getting error:

http://localhost:4200/assets/ts/utils.js?v=1.2 net::ERR_ABORTED 404 (Not Found)

Should I import to each component separately and if yes, what is the syntax ? Thanks.

The first thing is that what you are trying to import is a .js, not a ts.A 404 error is that you are not finding the file that you indicate in the specified path.

I recommend that you check the path and the name of the file so that they are exactly the same. Can you also try to remove ? V = 1.2 ? V = 1.2 .
If you really want to add a .ts file to your project, the import should be done through app.component.ts and not with an import in the Index.html

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