Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed import NgxEchartsModule in a Standalone Component #366

Open
s-triar opened this issue Jul 5, 2022 · 9 comments
Open

Failed import NgxEchartsModule in a Standalone Component #366

s-triar opened this issue Jul 5, 2022 · 9 comments

Comments

@s-triar
Copy link

s-triar commented Jul 5, 2022

I think this ngx-echarts v14 is still not support the standalone component feature.
the documentation does not provide how to import NgxEchartsModule in the standalone component.
I have imported NgxEchartsModule with forRoot in app.module.ts with the old bootstrap way.

if I import just NgxEchartsModule without root, appears error in echarts directive on the html tamplate.
if I import NgxEchartsModule with forRoot, appears error in imports section in @component.

@Component({
  selector: 'app-overview',
  templateUrl: './overview.component.html',
  styleUrls: ['./overview.component.scss'],
  standalone: true,
  imports: [
    CommonModule,
    RouterModule,
    FlexLayoutModule,
    MatCardModule,
    NgxEchartsModule
    // NgxEchartsModule.forRoot({
    //   echarts: () => import('echarts')
    // })
  ]
})
<div echarts [options]="options"  class="demo-chart"></div> <!-- errordirective echarts-->
@DaSchTour
Copy link

I guess it would be better to abandon the forRoot pattern and try to use injection tokens to provide the echarts library.

@s-triar
Copy link
Author

s-triar commented Dec 15, 2022

I am trying like this anf still getting error but different error.

@Component({
  selector: 'app-omzet-chart',
  standalone: true,
  templateUrl: './omzet-chart.component.html',
  styleUrls: ['./omzet-chart.component.scss'],
  imports: [
    CommonModule,
    MatFormFieldModule,
    MatSelectModule,
    ReactiveFormsModule,
    NgxEchartsModule
  ],
  providers:[
    DatePipe,
    {provide: NGX_ECHARTS_CONFIG, useFactory: () => import('echarts')},
    
  ]
})

the error in console log is
ERROR Error: Uncaught (in promise): TypeError: Cannot destructure property 'init' of 'undefined' as it is undefined. TypeError: Cannot destructure property 'init' of 'undefined' as it is undefined.

@s-triar
Copy link
Author

s-triar commented Dec 15, 2022

I found the way to fix it.

I am trying from this https://stackoverflow.com/questions/63176047/typeerror-cannot-destructure-property-init-of-object-null-as-it-is-null-in

I import echart in app.module
import * as echarts from 'echarts';
then add module like this

NgxEchartsModule.forRoot({
      echarts: { init: echarts.init }
    
    }),

then in standalone component I just add the NgxEchartsModule in imports

@tolgatopkara
Copy link

is there anyone can import with standalone components ?

@srikrishnaa0212
Copy link

srikrishnaa0212 commented Jan 30, 2023

we can try this way. It's working fine for me


import * as echarts from 'echarts';
import { NgxEchartsModule, NGX_ECHARTS_CONFIG } from 'ngx-echarts';

@Component({
  selector: 'bar-chart',
  template: `
    <div
      style="height: auto;"
      [id]="id"
      echarts
      [options]="chartOptions"
      [merge]="mergeOption"
      (chartInit)="chartOnInit($event)"
      (chartClick)="onClick($event)"
      (chartDblClick)="onChartDblClick($event)"
    ></div>
  `,
  standalone: true,
  imports: [NgxEchartsModule],
  providers:[{provide: NGX_ECHARTS_CONFIG, useFactory: () => ({echarts: echarts})}]
})

@rlmestre
Copy link

I am trying like this anf still getting error but different error.

@Component({
  ...
  providers:[
    DatePipe,
    {provide: NGX_ECHARTS_CONFIG, useFactory: () => import('echarts')},
    
  ]
})

the error in console log is ERROR Error: Uncaught (in promise): TypeError: Cannot destructure property 'init' of 'undefined' as it is undefined. TypeError: Cannot destructure property 'init' of 'undefined' as it is undefined.

In this example, the problem is the factory is returning the import directly instead of a config object. The correct statement would be:
{provide: NGX_ECHARTS_CONFIG, useFactory: () => ({ echarts: () => import('echarts') }) },

However in any case I suggest useValue to prevent the lib from being imported more than once if children try to inject it:
{provide: NGX_ECHARTS_CONFIG, useValue: { echarts: () => import('echarts') } },
Perhaps the README should be updated to use this method instead @xieziyu

we can try this way. It's working fine for me

import * as echarts from 'echarts';
import { NgxEchartsModule, NGX_ECHARTS_CONFIG } from 'ngx-echarts';

@Component({
  ...
  providers:[{provide: NGX_ECHARTS_CONFIG, useFactory: () => ({echarts: echarts})}]
})

By loading it this way, you lose the benefit of lazy-loading the library, so the entire package will be bundled with your component.

@sajouaou
Copy link

we can try this way. It's working fine for me


import * as echarts from 'echarts';
import { NgxEchartsModule, NGX_ECHARTS_CONFIG } from 'ngx-echarts';

@Component({
  selector: 'bar-chart',
  template: `
    <div
      style="height: auto;"
      [id]="id"
      echarts
      [options]="chartOptions"
      [merge]="mergeOption"
      (chartInit)="chartOnInit($event)"
      (chartClick)="onClick($event)"
      (chartDblClick)="onChartDblClick($event)"
    ></div>
  `,
  standalone: true,
  imports: [NgxEchartsModule],
  providers:[{provide: NGX_ECHARTS_CONFIG, useFactory: () => ({echarts: echarts})}]
})

When I use this solution i have this error

Property 'chartOptions' does not exist on type

@semla
Copy link

semla commented Sep 22, 2023

iiuc this imports the whole bundle? how to only import used parts?

@simon04
Copy link

simon04 commented Nov 26, 2023

Fixed via #411. Here is the documentation: https://github.com/xieziyu/ngx-echarts/tree/v17.1.0#standalone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants