angular.2在index.html中引用外部js文件

问题描述:

我正在使用angular cli工具进行简单的angular 2应用程序.在我的代码中,我必须在index.html中包含jquery.js文件.jquery.js位于node_modules目录下,但该语句

I am making a simple angular 2 application using angular cli tool. In my code I have to include jquery.js file in my index.html. jquery.js is under node_modules directory but the statement

< script src ="../node_modules/jquery/dist/jquery.js"></script>

似乎不起作用:

无法加载资源: http://localhost:4200/node_modules/jquery/dist/jquery.js 服务器响应状态为404(未找到)

Failed to load resource: http://localhost:4200/node_modules/jquery/dist/jquery.js the server responded with a status of 404 (Not Found)

如何确定jquery.js包含在index.html中?感谢您的帮助.

How can I make sure that the jquery.js is included in index.html? Thanks for any help.

为了包括全局库,您必须在angular-cli的 scripts 数组中添加jquery.js文件.json:

In order to include a global library, you have to add the jquery.js file in the scripts array from angular-cli.json:

"scripts": [
  "../node_modules/jquery/dist/jquery.js"
]

此后,如果已启动 ng serve ,请重新启动.

After this, restart ng serve if it is already started.