林世哲 2019-08-30 14:34 采纳率: 0%
浏览 392

typescript里怎么用h5新标签?

最近要写一个拍照功能,angular+ts,在用h5标签的时候(video, canvas)出现了问题

canvas为例:
如果我这么写

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

就会报错:

 Property 'getContext' does not exist on type 'HTMLElement'.

如果我这么写

@ViewChild('canvas') private canvas: HTMLCanvasElement;

运行的时候报

ERROR TypeError: this.canvas.getContext is not a function

我服了,video标签也是这样
应该怎么办啊,求助各位大佬!!!!!!

  • 写回答

1条回答 默认 最新

  • 林世哲 2019-08-30 14:58
    关注

    解决了
    这么写:
    @ViewChild('canvas') private canvas: ElementRef;
    用的时候:
    this.canvas.nativeElement.getContext('2d');
    忘记写nativeElement了,佛了,难住了我两天,快哭了md

    评论

报告相同问题?