首页 > 解决方案 > ng2-Chart 不适用于新创建的 Angular 10 项目

问题描述

尝试将 ng2-charts 添加到新创建的角度项目时,会出现此错误

编译时似乎是个问题

我尝试并搜索了多种方法,但没有运气

错误

这是我的 package.json

  "private": true,
  "dependencies": {
    "@angular/animations": "~10.1.0-next.7",
    "@angular/common": "~10.1.0-next.7",
    "@angular/compiler": "~10.1.0-next.7",
    "@angular/core": "~10.1.0-next.7",
    "@angular/forms": "~10.1.0-next.7",
    "@angular/platform-browser": "~10.1.0-next.7",
    "@angular/platform-browser-dynamic": "~10.1.0-next.7",
    "@angular/router": "~10.1.0-next.7",
    "chart.js": "^2.9.3",
    "ng2-charts": "^2.4.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
    "devDependencies": {
    "@angular-devkit/build-angular": "~0.1001.0-next.6",
    "@angular/cli": "~10.1.0-next.6",
    "@angular/compiler-cli": "~10.1.0-next.7",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.5"
  }

这是 app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ChartsModule } from 'ng2-charts/lib/charts.module';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ChartsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

标签: angulartypescriptchart.jsng2-charts

解决方案


尝试更改导入ChartsModule如下:

import { ChartsModule } from 'ng2-charts';

请查看此StackBlitz以查看使用 Angular 10 的工作示例。


推荐阅读