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

6
我正在尝试将hello.js包含在我的Angular 5.0.2项目中。
以下是cli版本。

enter image description here

我已经将脚本文件添加到angular-cli.json文件中。
"scripts": [
        "./js/hello.js",
        "./js/hello.polyfill.js",
       ]

路径是正确的,因为我也在angular-cli.json中加载样式表,它们可以正常加载。
在我的服务文件中,我像下面这样导入hello:
declare var hello: any;
declare var gapi: any;

当我运行ng build时,控制台显示错误: 无法找到模块“hello”。 如果我通过script标签在index.html中加载文件,则代码和导入工作正常。只有当我将其添加到angular-cli.json文件中时,它才停止工作。
请指导 谢谢

1
为什么不为hello.js安装类型?npm i -D @types/hellojs - filoxo
2
你的文件夹结构是什么样的?尝试将你的JS文件放在assets/js文件夹中,并提供路径assets/js/hello.js - ricky
https://github.com/angular/angular-cli/wiki/stories-third-party-lib - R. Richards
1个回答

3

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

您还必须指定 assets 文件夹:

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

这是答案吗? - sg7
你可以在哪个文件中发布? - Khurshid Ansari
.angular-cli.json - Denis Nutiu
我提到了正确的路径,但是当我尝试导入时,它显示错误,无法找到模块。 - gauti
请注意,angular-cli.json现在已更名为angular.json。 - Denis Nutiu
如果您想将其作为模块导入,则应使用npm进行安装。 - Denis Nutiu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接