最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Include External js file to angular 5.0.0 project - Stack Overflow

programmeradmin4浏览0评论

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

Below is the cli version

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

"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.

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

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

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

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.

Please guide Thanks

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

Below is the cli version

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

"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.

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

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

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

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.

Please guide Thanks

Share Improve this question asked Nov 24, 2017 at 5:58 Shruti NairShruti Nair 2,0349 gold badges32 silver badges60 bronze badges 3
  • 1 Why not install the types for hello.js? npm i -D @types/hellojs – filoxo Commented Nov 24, 2017 at 6:03
  • 2 what is your folder structure? try putting you js file inside assets/js folder and provide path assets/js/hello.js – ricky Commented Nov 24, 2017 at 6:14
  • github./angular/angular-cli/wiki/stories-third-party-lib – R. Richards Commented Nov 24, 2017 at 12:01
Add a ment  | 

1 Answer 1

Reset to default 3

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

You must also specify the assets folder:

"scripts": [
        "./assets/js/hello.js",
        "./assets/js/hello.polyfill.js",
       ]
发布评论

评论列表(0)

  1. 暂无评论