如何在Angular 6中使用外部JS文件 [英] How to use external JS files in Angular 6

查看:391
本文介绍了如何在Angular 6中使用外部JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Angular开始了一个项目,但是我从未想过安装最新版本会给我带来一些问题。我也在使用Materialize,所以当我尝试'导入'它的Javascript文件时,它不起作用。我不知道为什么,我从上周五开始寻找答案,但我一无所获。

I started a project with Angular but I never thought that install the most recent version of it would bring me a couple of problems. I am also using Materialize so when I try to 'import' its Javascript file it doesn't work. I don't know why, I was looking for an answer since the past Friday but I found nothing.

我已经更改了angular.json文件并在其中引用了我的JS位置,但这还不够。

I have changed the angular.json file and referenced my JS location in it but it doesn't was sufficient.

PD我不能使用CDN来实现JS。

P.D. I must not use the CDN for materialize JS.

推荐答案

角度为6的CLI项目使用 angular。 json 而不是 .angular-cli.json 用于构建和项目配置。这意味着您正在使用Angular 6.


从v6开始,文件的位置已更改为 angular.json 。由于不再有前导点,因此默认情况下不再隐藏文件并且该文件位于同一级别。
这也意味着angular.json中的文件路径不应包含前导点和斜线,即你应该提供绝对路径

CLI projects in angular 6 onwards uses angular.json instead of .angular-cli.json for build and project configuration. That implies you are using Angular 6.
As of v6, the location of the file has changed to angular.json. Since there is no longer a leading dot, the file is no longer hidden by default and is on the same level. which also means that file paths in angular.json should not contain leading dots and slash i.e you should provide an absolute path

安装MaterialiseCSS和angular2 -bterialize from npm

Install MaterializeCSS and angular2-materialize from npm

 npm install materialize-css --save 
 npm install angular2-materialize --save 
 npm install jquery@^2.2.4 --save
 npm install hammerjs --save

安装所有必需的依赖项后,将它们添加到样式和脚本数组 angular.json

After installing all the required dependencies add them to styles and scripts array of angular.json

"styles": [

      "src/styles.css",
      "node_modules/materialize-css/dist/css/materialize.css"
],
"scripts": [
      "node_modules/jquery/dist/jquery.js",
       "node_modules/hammerjs/hammer.js",
       "node_modules/materialize-css/dist/js/materialize.js"
 ]

这篇关于如何在Angular 6中使用外部JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆