简体   繁体   English

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

[英]Include External js file to angular 5.0.0 project

I'm trying to include hello.js in my angular 5.0.2 project. 我试图在我的角度5.0.2项目中包含hello.js

Below is the cli version 以下是cli版本

在此输入图像描述

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'. 找不到模块'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 请指导谢谢

Edit the scripts array in angular-cli.json or angular.json. 在angular-cli.json或angular.json中编辑脚本数组。

You must also specify the assets folder: 您还必须指定assets文件夹:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM