NullInjectorError:没有InjectionToken NGX_ECHARTS_CONFIG的提供程序

时间:2020-09-11 09:17:59

标签: angular charts angular-chart

我正在开发角度应用程序,当我打开项目时,出现如下错误

ERROR NullInjectorError: R3InjectorError(IndexModule)[InjectionToken NGX_ECHARTS_CONFIG -> InjectionToken NGX_ECHARTS_CONFIG -> InjectionToken NGX_ECHARTS_CONFIG -> InjectionToken NGX_ECHARTS_CONFIG]: 
  NullInjectorError: No provider for InjectionToken NGX_ECHARTS_CONFIG!
    at NullInjector.get (http://localhost:4200/vendor.js:71789:21)
    at R3Injector.get (http://localhost:4200/vendor.js:84254:29)
    at R3Injector.get (http://localhost:4200/vendor.js:84254:29)
    at R3Injector.get (http://localhost:4200/vendor.js:84254:29)
    at NgModuleRef$1.get (http://localhost:4200/vendor.js:100306:31)
    at R3Injector.get (http://localhost:4200/vendor.js:84254:29)
    at NgModuleRef$1.get (http://localhost:4200/vendor.js:100306:31)
    at Object.get (http://localhost:4200/vendor.js:97671:29)
    at getOrCreateInjectable (http://localhost:4200/vendor.js:75308:31)
    at Module.ɵɵdirectiveInject (http://localhost:4200/vendor.js:87509:10)

尝试安装ngx-echarts

npm install echarts -S
npm install ngx-echarts -S

仍然出现上述错误

1 个答案:

答案 0 :(得分:3)

NgxEchartsModule导入您的应用程序模块(或其他任何适当的角度模块):

具有动态导入功能

import { NgxEchartsModule } from 'ngx-echarts';

@NgModule({
  imports: [
    ...,
    NgxEchartsModule.forRoot({
      echarts: () => import('echarts')
    })
  ],
})
export class AppModule { }

OR

您也可以直接传递echart,这会减慢初始渲染的速度,因为它将整个echart加载到您的主包中。

import { NgxEchartsModule } from 'ngx-echarts';

import * as echarts from 'echarts';

@NgModule({
  imports: [
    NgxEchartsModule.forRoot({
      echarts,
    }),
  ],
})
export class AppModule {}