我想在Angular项目中使用Chart.js。 在之前的Angular2版本中,我使用'chart.loader.ts'做得很好,它有:
export const { Chart } = require('chart.js');
然后在组件代码中我只是
import { Chart } from './chart.loader';
但升级到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
在我的'tsconfig.json'中,我有
"typeRoots": [
"node_modules/@types"
],
在'node_modules/@types/node/index.d.ts'中有:
declare var require: NodeRequire;
所以我很困惑。
顺便说一句,我经常遇到警告:[tslint] The selector of the component "OverviewComponent" should have prefix "app"(component-selector)
虽然我在'.angular-cli.json'中设置了“prefix”:“”。可能是因为从'angular-cli.json'改为'.angular-cli.json'的原因?
答案 0 :(得分:186)
问题(如typescript getting error TS2304: cannot find name ' require'中所述)是未安装节点的类型定义。
对于预计的with @angular/cli 1.x
,具体步骤应为:
第1步:
使用以下任一方式安装@types/node
:
- npm install --save @types/node
- yarn add @types/node -D
第2步:
编辑 src / tsconfig.app.json 文件并添加以下内容代替空"types": []
,它应该已存在:
...
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
...
如果我遗漏了任何内容,请发表评论,然后我会编辑我的答案。
答案 1 :(得分:20)
最后我得到了解决方案,检查我的App模块文件:
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from '@angular/material';
import 'hammerjs';
import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';
import { AppRouting } from './app.routing';
import { AppComponent } from './app.component';
declare var require: any;
export function highchartsFactory() {
const hc = require('highcharts');
const dd = require('highcharts/modules/drilldown');
dd(hc);
return hc;
}
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRouting,
BrowserAnimationsModule,
MaterialModule,
ChartModule
],
providers: [{
provide: HighchartsStatic,
useFactory: highchartsFactory
}],
bootstrap: [AppComponent]
})
export class AppModule { }
请注意上述代码中的declare var require: any;
。
答案 2 :(得分:13)
仍然不确定答案,但可能的解决方法是
import * as Chart from 'chart.js';
答案 3 :(得分:10)
至于我,使用VSCode和Angular 5,只需要在tsconfig.app.json中为类型添加“node”。 保存并重新启动服务器。
{
"compilerOptions": {
..
"types": [
"node"
]
}
..
}
一个奇怪的是,这个问题“找不到要求(”,用ts-node激活时不会发生
答案 4 :(得分:4)
我遇到了同样的问题,我正在添加
"types": ["node"]
到根文件夹的 tsconfig.json 。
在 src 文件夹下还有一个 tsconfig.app.json ,我通过添加
解决了这个问题"types": ["node"]
转到compilerOptions
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": ["node"] ----------------------< added node to the array
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
答案 5 :(得分:2)
我添加了
"types": [
"node"
]
在我的 tsconfig 文件中,它对我有用 tsconfig.json 文件看起来像
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": [
"node",
"underscore"
]
},
答案 6 :(得分:1)
我将tsconfig.json
文件移动到新文件夹中以重构我的项目。
因此,它无法解析tsconfig.json的typeRoots
属性中的node_modules / @ types文件夹的路径
所以只需从
更新路径"typeRoots": [
"../node_modules/@types"
]
到
"typeRoots": [
"../../node_modules/@types"
]
确保从tsconfig.json的新位置解析node_modules的路径
答案 7 :(得分:0)
在出现错误的文件顶部添加以下行:
declare var require: any