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

时间:2019-09-06 15:55:00

标签: javascript html 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)

1 个答案:

答案 0 :(得分:0)

以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脚本中。