Tho*_*ang 113 angular-cli angular
我想在我的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'的原因?
Ind*_*ill 211
问题(如获取错误TS2304:找不到名称'require'的原型中所述)是未安装节点的类型定义.
对于一个预计的genen with @angular/cli 1.x,具体步骤应该是:
第1步:
@types/node使用以下任一方法安装:
- npm install --save @types/node
- yarn add @types/node -D
Run Code Online (Sandbox Code Playgroud)
第2步:编辑你的src/tsconfig.app.json文件并添加以下代替空"types": [],应该已经存在:
...
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
...
Run Code Online (Sandbox Code Playgroud)
如果我错过任何内容,请发表评论,我会编辑我的答案.
GsM*_*tra 22
最后我得到了解决方案,检查我的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 { }
Run Code Online (Sandbox Code Playgroud)
请注意declare var require: any;上面的代码.
Tho*_*ang 13
仍然不确定答案,但可能的解决方法是
import * as Chart from 'chart.js';
Run Code Online (Sandbox Code Playgroud)
Val*_*nez 12
至于我,使用VSCode和Angular 5,只需要在tsconfig.app.json中为类型添加"node".保存,然后重新启动服务器.
{
"compilerOptions": {
..
"types": [
"node"
]
}
..
}Run Code Online (Sandbox Code Playgroud)
一个奇怪的事情是,这个问题"找不到要求(",在用ts节点进行切换时不会发生
Sun*_*arg 11
I was facing the same issue, I was adding
"types": ["node"]
Run Code Online (Sandbox Code Playgroud)
to tsconfig.json of root folder.
There was one more tsconfig.app.json under src folder and I got solved this by adding
"types": ["node"]
Run Code Online (Sandbox Code Playgroud)
to tsconfig.app.json file under compilerOptions
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": ["node"] ----------------------< added node to the array
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87196 次 |
| 最近记录: |