[Chartjs]-Ionic/Chart.js – Cannot read property 'nativeElement' of undefined

0πŸ‘

βœ…

In home.ts you are loading the bar in ionViewDidLoad so if u wanted to put it in such way ,
u can’t put it in the segment since the segment will not be able to read the ionViewDidLoad inner data since segment is also being loaded after the ionViewDidLoad function is executed,else if you want to put it in the segment,
you should take the data out from the ionViewDidLoad() and put them in other function and therefore u could put the function directly in segment and thus the function could be readable by the segment.

4πŸ‘

If you are working with Angular >=8, now you have to pass a second parameter to Viewchild for options and set static: true

@ViewChild('barcanvas', {static: true}) barcanvas;

0πŸ‘

ionViewDidLoad() dont use this. Just use this : ionViewDidEnter().

0πŸ‘

Another possible answer if someone is having this issue :
in the template if the chart canvas is within a condition (if, ng-template, switch, etc), it might not find the nativeElement. You can put a timer to give the time to the browser to resolve it. Ex in a .ts file :

setTimeout(() => {
  this.ionViewDidLoad();
}, 50);

In this present issue the canvas is within a switch case.

Leave a comment