Vidhya Sagar
Vidhya Sagar

Reputation: 33

My Local JavaScript file is not loaded in Angular 2

I have added a local javascript file with <script type='text/javascript' src='Test.js'> in my index.html, but when I run it with ng serve and go to localhost:4200, in my browser console I see this error:

GET localhost:4200/Test.js 404 (Not Found) but I do have my file in the same location that my index.html.

but angular 2 not loading or finding this. Please help me.

Upvotes: 1

Views: 3500

Answers (1)

Juan
Juan

Reputation: 6383

Move your Test.js file to the assets folder. Then from index.html, point to that folder <script type='text/javascript' src='./assets/Test.js' >

WHY? When you serve your app with ng serve, there's a structure set for it. If you want to see it, run ng build, you will see a dist folder being created.

Observe the assets folder gets move to that dist folder, and that's how the app expects it. So you are getting the not found error because from the server's perspective, there's no such file there.

When you make those changes and run ng serve, things should work and error should go away.

Hope this is of help.

Upvotes: 8

Related Questions