TypeError: Cannot read property 'nativeElement' of undefined in HTML5 Canvas with Ionic 2

my home.ts looks like this


import { Component,ViewChild,ElementRef} from '@angular/core';

    export class HomePage {
      @ViewChild('canvas') canvasEl : ElementRef;
      private _CANVAS:any;
      private _CONTEXT:any;
    }

    ionViewDidLoad() {
        this._CANVAS = this.canvasEl.nativeElement;
        this._CONTEXT = this._CANVAS.getContext('2d');
      }
    }

this is what I have in home.html

<canvas #canvas></canvas>

ERROR TypeError: Cannot read property ‘nativeElement’ of undefined. I keep getting this error and I dont know how to solve. Could someone help me on this Thank you so much.

Hello,
hmmm documentation says dom is ready in memory. Maybe you could use a other lifecyle.

ngAfterViewInit() {

{

Best regards, anna-liebt

4 Likes

It was working. Thank you so much ^^

Can you be more specific in your answer , please ?