javascript 将外部 js 文件包含到 angular 5.0.0 项目中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47467163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:23:50  来源:igfitidea点击:

Include External js file to angular 5.0.0 project

javascriptangularangular-cli

提问by Shruti Nair

I'm trying to include hello.jsin my angular 5.0.2 project.

我正在尝试将hello.js包含在我的 angular 5.0.2 项目中。

Below is the cli version

下面是cli版本

enter image description here

在此处输入图片说明

I have added the script file to the angular-cli.json file.

我已将脚本文件添加到 angular-cli.json 文件中。

"scripts": [
        "./js/hello.js",
        "./js/hello.polyfill.js",
       ]

The path is correct as i'm also loading style in the angular-cli.json which are loading fine.

路径是正确的,因为我还在 angular-cli.json 中加载了加载良好的样式。

In my service file i'm importing hello as below:

在我的服务文件中,我导入 hello 如下:

declare var hello: any;
declare var gapi: any;

but when i run ng build the console shows the error:

但是当我运行 ng build 时,控制台显示错误:

Cannot find module 'hello'.

找不到模块“你好”。

If i load the files through script tag in the index.html the code and imports works fine .Only when i add it to the angular-cli.json file it stops working.

如果我通过 index.html 中的脚本标签加载文件,代码和导入工作正常。只有当我将它添加到 angular-cli.json 文件时,它才会停止工作。

Please guide Thanks

请指导谢谢

采纳答案by Metonymy

Edit the scripts array in angular-cli.json or angular.json.

编辑 angular-cli.json 或 angular.json 中的脚本数组。

You must also specify the assets folder:

您还必须指定资产文件夹:

"scripts": [
        "./assets/js/hello.js",
        "./assets/js/hello.polyfill.js",
       ]