我的本地JavaScript文件未在Angular 2中加载 [英] My Local JavaScript file is not loaded in Angular 2

查看:63
本文介绍了我的本地JavaScript文件未在Angular 2中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 index.html 中添加了< script type ='text/javascript'src ='Test.js'> 的本地javascript文件,但是当我用ng serve运行它并转到 localhost:4200 时,在我的浏览器控制台中,我看到此错误:

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(未找到),但是我确实有文件与我的index.html相同的位置.

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

但角度2未加载或未找到.请帮助我.

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

推荐答案

将您的 Test.js 文件移动到资产文件夹中.然后从 index.html 指向该文件夹< script type ='text/javascript'src ='./assets/Test.js'>

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' >

为什么?当您使用 ng serve 服务您的应用程序时,会为其设置一个结构.如果要查看它,请运行 ng build ,将看到正在创建的 dist 文件夹.

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.

观察资产文件夹将移动到该dist文件夹,这就是应用程序期望的方式.因此,您将得到未找到的错误,因为从服务器的角度来看,那里没有此类文件.

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.

进行这些更改并运行 ng服务时,事情应该起作用并且错误应该消失.

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

希望这对您有所帮助.

这篇关于我的本地JavaScript文件未在Angular 2中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆