我想在我的Angular 项目中使用Chart.js. 在以前的Angular2版本中,我已经通过使用‘chart.loader.ts’很好地做到了这一点,它具有:

export const { Chart } = require('chart.js');

然后在组件代码中

import { Chart } from './chart.loader';

但是在升级到cli 1.0.0和Angular 4之后,我收到错误:"找不到名称‘REQUIRED’".

要重现错误,请执行以下操作:

ng new newapp
cd newapp
npm install chart.js --save
echo "export const { Chart } = require('chart.js');" >> src/app/chart.loader.ts
ng serve

在我的电脑里.是的,我有

"typeRoots": [
  "node_modules/@types"
],

在'node_modules/@types/node/index'中.d、 有:

declare var require: NodeRequire;

所以我很困惑.

顺便说一句,我经常遇到这样的警告:

[tslint] The selector of the component "OverviewComponent" should have prefix "app"(component-selector)

虽然我已经在我的字典里设置了前缀":".好的.json'.可能是因为从"Angular "改变了.json"to".好的.原因是什么?

推荐答案

The problem (as outlined in typescript getting error TS2304: cannot find name ' require') is that the type definitions for node are not installed.

对于预测的第with @angular/cli 1.x代,具体步骤应为:

Step 1:

使用以下任一设备安装@types/node:

- npm install --save @types/node
- yarn add @types/node -D

Step 2: 编辑您的src/tsconfig.app.json文件,并添加以下内容来代替应该已经存在的空"types": []:

...
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
...

如果我遗漏了什么,请写下 comments ,我会编辑我的答案.

Angular相关问答推荐

间歇性不正确的SSR重定向(server. ts级别的请求和响应不匹配)

有没有一种方法用timeout()操作符创建计数器,用于超时一个观察对象?

PrimeNG面包屑组件生成奇怪的&

MAT表的内联文本编辑-未刷新编辑图标

向Anguar 17项目添加服务以启动和停止Loader-NGX-UI-Loader

Angular -移位在2个示波器中读取

从Angular 前端访问ASP.NET Core 8 Web API时出现CORS错误

无法创建新的 Ionic 项目

如何根据响应变化更改 Angular Carousel 的cellsToShow

模块内的命名路由出口 - 路径匹配但内容未加载

Angular UNIVERSAL prerender 错误方法 Promise.prototype.then 调用不兼容的接收器 [object Object]

Nz 树 Select .如何在子 node 中显示父 node 名称?

等待两个订阅完成而不嵌套

使用 RxJS forkJoin 时传递参数的最佳方式

类继承支持

Angular-CLI 代理到后端不起作用

Angular:找不到不同的支持对象[object Object]

如何在功能模块层次 struct 中使用 .forRoot()

没有 ChildrenOutletContexts 的提供者 (injectionError)

RxJS - 发生错误时观察到的不会完成