错误TypeError:无法读取未定义的属性'nativeElement'

Sachith Nanayakkara

我是Angle 2的新手,我致力于图像裁剪插件,我想在画布上显示图像。这是我的html代码

<div class="row">
  <div class="col-sm-6">
     <canvas id="layout" width="400" height="300">
        This text is displayed if your browser does not support HTML5 Canvas.
     </canvas>

这是我的打字稿文件

import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'app-four',
templateUrl: './four.component.html',
styleUrls: ['./four.component.css']
})
export class FourComponent implements OnInit {
image: HTMLImageElement;
profPic: any;
context: CanvasRenderingContext2D;

constructor() { }
@ViewChild('layout') canvas;
@ViewChild('photo') photo;

ngOnInit() {
const _canvas = this.canvas.nativeElement;
//let photo = this.photo.nativeElement;
this.context = (<HTMLCanvasElement>_canvas).getContext('2d');
  this.image = new Image();
  this.image.src = '../../assets/images/1.jpg';

this.context.drawImage(this.image,20,20,500,260);

}
}

我有错误

错误TypeError:无法读取未定义的属性'nativeElement'

请帮我解决这个问题

谢谢

Santosh Singh

使layout本地模板变量

 <canvas #layout id="layout" width="400" height="300">
        This text is displayed if your browser does not support HTML5 Canvas.
     </canvas>

工作演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular @ViewChild错误'无法读取未定义的属性nativeElement”

打字稿错误“无法读取未定义的属性‘nativeElement’”?

错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ nativeElement”

无法读取未定义的属性“ nativeElement”-ngAfterViewInit

错误类型错误:由于 ng-template,无法读取未定义的属性“nativeElement”

错误:使用离子路由时无法读取未定义的属性'nativeElement'

如何解决使用ViewChild ElementRef时无法读取angular中未定义的属性'nativeElement'

vimeo / player.js-无法读取未定义的属性'nativeElement'

Ionic / Chart.js-无法读取未定义的属性“ nativeElement”

Angular 2 ViewChild不起作用-无法读取未定义的属性nativeElement

@viewChild不起作用-无法读取未定义的属性nativeElement

Angular 6:TypeError:无法获取未定义或空引用的属性“ nativeElement”

Karma + Jasmine - 默认单元测试“它应该创建”失败并出现错误“未捕获的类型错误:无法读取未定义的属性‘nativeElement’”

为什么elementRef的nativeElement未定义?

角度:在ViewChild上未定义nativeElement

角模态未定义nativeElement

为什么未定义nativeElement?

离子为什么nativeElement未定义?

角度测试:无法读取null的属性“ nativeElement”

反应 - 错误:TypeError:无法读取未定义的属性(读取“then”)

“TypeError:无法读取未定义的属性(读取'hasOwnProperty')”错误

我的模板参考变量.nativeElement未定义

ViewChildren 的第一个 nativeElement 未定义

使用@ViewChild保留输入的.nativeElement会返回“未定义”

如果内容用 ngif 包裹,则 viewchild nativeElement 未定义

作为错误:TypeError:无法读取未定义的属性“位置”

TypeError中的错误:无法读取未定义的属性“标志”

错误TypeError:无法读取未定义的属性'length'

错误TypeError:无法读取未定义的属性“调用”