繁体   English   中英

如何在angular中导入外部javascript库?

[英]How to import an external javascript library in angular?

我正在尝试导入诸如popper,jquery和chart.js之类的库以在有角度的项目中使用。

我使用cli下载了它们,并在我的index.htmlangular.json文件中引用了它们。

index.html

<head>
...
<script src="node_modules/chart.js/dist/Chart.js"></script>
</head>
angular.json
...
"styles": [
              "node_modules/bootstrap/dist/css/bootstrap.css",
              "node_modules/font-awesome/css/font-awesome.css",
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/chart.js/dist/Chart.js"
            ]
...

出现的错误信息是

The resource from “http://localhost:4200/node_modules/chart.js/dist/Chart.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)

以Chart.js为例:

npm install --save chart.js
npm install --save-dev @types/chart.js

然后在您的组件中:

import * as Chart from 'chart.js'

...

const chart = new Chart(...)

无需触摸index.html或将任何内容放入您的angular.json脚本中。

暂无
暂无

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

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