admin管理员组

文章数量:1391943

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.

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.

Share Improve this question edited Jun 25, 2017 at 15:09 Saurabh Bhandari 2,4584 gold badges28 silver badges33 bronze badges asked Jun 24, 2017 at 20:26 Vidhya SagarVidhya Sagar 331 silver badge11 bronze badges 3
  • What error do you get... – Juan Commented Jun 24, 2017 at 20:29
  • GET localhost:4200/Test.js 404 (Not Found) I get this error. – Vidhya Sagar Commented Jun 24, 2017 at 20:30
  • 1 Wele to Stalk Overflow. Update that as part of your question before you get a wave of downvotes. See this: stackoverflow./help/how-to-ask. And when you get an answer that is acceptable to you, don't forget to mark it as so: stackoverflow./help/someone-answers. – Juan Commented Jun 24, 2017 at 20:40
Add a ment  | 

1 Answer 1

Reset to default 8

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.

本文标签: My Local JavaScript file is not loaded in Angular 2Stack Overflow