Skip to content

ngx-echarts directive contructor NgxEchartsConfig is null with Angular 9 #241

Open
@joniruokamo

Description

@joniruokamo

I have tried the new v5.0.0 with Angular 9

configuration:

  • Angular v 9.1.0
  • ngx-echarts v5.0.0
  • echarts v4.8.0

And I get the following error:

core.js:6185 ERROR TypeError: Cannot read property 'init' of null

I see this comes from the line:

return this.ngZone.runOutsideAngular(() => this.echarts.init(dom, this.theme, this.initOpts));

where this.echarts is null.

I tried to modify the newly added .forRoot() in the app.module.ts with instructions from #236 and #237. This did not help.

Screenshot 2020-06-10 at 15 14 26

Activity

ICTlight

ICTlight commented on Jun 23, 2020

@ICTlight

deal??

cray2015

cray2015 commented on Jun 23, 2020

@cray2015

the workaround for this issue is to downgrade echarts for the time being.

    "ngx-echarts": "^4.2.2",
    "echarts": "^4.2.1",

These version worked for me in the production environment along with Angular 9

Reda1000

Reda1000 commented on Jun 24, 2020

@Reda1000

Can confirm - upgraded Angular yesterday and found echarts missing today in production build via ng build --prod.

Allthough using ng serve it does display charts correctly.

FonsecaJoao

FonsecaJoao commented on Jun 29, 2020

@FonsecaJoao

the workaround for this issue is to downgrade echarts for the time being.

    "ngx-echarts": "^4.2.2",
    "echarts": "^4.2.1",

These version worked for me in the production environment along with Angular 9

For those who want to apply this workaround, it is worth mentioning that the import in appModule needs to be like this:

imports: [
    ...,
    NgxEchartsModule,
],

instead of this:

imports: [
    ...,
    NgxEchartsModule.forRoot({
      echarts,    
    }),
],
cray2015

cray2015 commented on Jun 30, 2020

@cray2015

the workaround for this issue is to downgrade echarts for the time being.

    "ngx-echarts": "^4.2.2",
    "echarts": "^4.2.1",

These version worked for me in the production environment along with Angular 9

For those who want to apply this workaround, it is worth mentioning that the import in appModule needs to be like this:

imports: [
    ...,
    NgxEchartsModule,
],

instead of this:

imports: [
    ...,
    NgxEchartsModule.forRoot({
      echarts,    
    }),
],

Yes my bad. The only reason I did not mention that is because the build will give error if you still use forRoot, also the previous version of ngx-echarts never had forRoot so I thought it was self explanatory. :)

lundmikkel

lundmikkel commented on Jun 30, 2020

@lundmikkel

I seem to have found a fix for this, when using version 5.0.0. As far as I can see, the problem arises with AOT (ahead-of-time compilation) being used. The dependency gets optimized away, somehow. The trick is to rewrite the import like this:

imports: [
    ...,
    NgxEchartsModule.forRoot({
      echarts: { init: echarts.init }
    })
],

Not quite sure why it works, however.

lundmikkel

lundmikkel commented on Jul 1, 2020

@lundmikkel

I seem to have found a fix for this, when using version 5.0.0.

This actually seems to cause type problems elsewhere, so might not really be a solution after all.

VitalyName

VitalyName commented on Dec 10, 2020

@VitalyName

I solved it in my application. I make this steps:

  1. Delete import
    import * as echarts from 'echarts';

  2. Add function with import to app.module.ts

export function chartModule(): any {
    return import('echarts');
}
  1. Add to import section in app.module.ts
    imports: [
        // Other imports

        NgxEchartsModule.forRoot({
            echarts: chartModule
        })
    ],
Arberto99

Arberto99 commented on Dec 6, 2021

@Arberto99

I solved it in my application. I make this steps:

  1. Delete import
    import * as echarts from 'echarts';
  2. Add function with import to app.module.ts
export function chartModule(): any {
    return import('echarts');
}
  1. Add to import section in app.module.ts
    imports: [
        // Other imports

        NgxEchartsModule.forRoot({
            echarts: chartModule
        })
    ],

Useful, but found a better fix for the import in module:

NgxEchartsModule.forRoot({
  echarts: () => import('echarts'),
}),

without any function in app.module.ts, so the code is cleaner. :)

BhagyashreeCH

BhagyashreeCH commented on Feb 6, 2023

@BhagyashreeCH

I seem to have found a fix for this, when using version 5.0.0. As far as I can see, the problem arises with AOT (ahead-of-time compilation) being used. The dependency gets optimized away, somehow. The trick is to rewrite the import like this:

imports: [
    ...,
    NgxEchartsModule.forRoot({
      echarts: { init: echarts.init }
    })
],

Not quite sure why it works, however.

I have recently upgraded angular 7 to 9 even after tried to update multiple versions of ngx-echarts and echarts versions I was getting NgxEchartsConfig is null error, finally i was able to fix with your solution as mentioned above.

Angular: 9.1.13
"echarts": "^4.9.0",
"ngx-echarts": "^6.0.0",

NgxEchartsModule.forRoot({
echarts: { init: echarts.init }
})
Thank you sir lundmikkel...

rickynguyenc

rickynguyenc commented on Apr 27, 2023

@rickynguyenc

Thanks you so much, It work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lundmikkel@VitalyName@ICTlight@Reda1000@cray2015

        Issue actions

          ngx-echarts directive contructor NgxEchartsConfig is null with Angular 9 · Issue #241 · xieziyu/ngx-echarts