我想在我的Angular项目中使用Chart.js.在以前的Angular2版本中,我使用'chart.loader.ts'做得很好,它有:
export const { Chart } = require('chart.js');
Run Code Online (Sandbox Code Playgroud)
然后在组件代码中我就是
import { Chart } from './chart.loader';
Run Code Online (Sandbox Code Playgroud)
但升级到cli 1.0.0和Angular 4之后,我收到错误:"找不到名称'require'".
要重现错误:
ng new newapp
cd newapp
npm install chart.js --save
echo "export const { Chart } = require('chart.js');" >> src/app/chart.loader.ts
ng serve
Run Code Online (Sandbox Code Playgroud)
在我的'tsconfig.json'中,我有
"typeRoots": [
"node_modules/@types"
],
Run Code Online (Sandbox Code Playgroud)
在'node_modules/@types/node/index.d.ts'中有:
declare var require: NodeRequire;
Run Code Online (Sandbox Code Playgroud)
所以我很困惑.
顺便说一句,我经常遇到警告:
[tslint] The selector of the component "OverviewComponent" should have prefix "app"(component-selector)
Run Code Online (Sandbox Code Playgroud)
虽然我在'.angular-cli.json'中设置了"前缀":"".可能是因为从'angular-cli.json'改为'.angular-cli.json'的原因?