Angular 角度类型错误问题中的视频

Angular 角度类型错误问题中的视频,angular,typescript,html5-video,Angular,Typescript,Html5 Video,我正在尝试用Angular做一些基本的视频流工作。我的代码如下。挑战是我不断地得到和错误,说。。。 错误:未捕获(承诺中):TypeError:无法读取未定义的属性“video” TypeError:无法读取未定义的属性“video” 如有任何建议,将不胜感激 导入{ 组成部分, 奥尼特, ViewChild, ElementRef }从“@angular/core”开始; @组成部分({ 选择器:“应用程序扫描程序”, templateUrl:'./scanner.component.htm

我正在尝试用Angular做一些基本的视频流工作。我的代码如下。挑战是我不断地得到和错误,说。。。 错误:未捕获(承诺中):TypeError:无法读取未定义的属性“video” TypeError:无法读取未定义的属性“video”

如有任何建议,将不胜感激

导入{
组成部分,
奥尼特,
ViewChild,
ElementRef
}从“@angular/core”开始;
@组成部分({
选择器:“应用程序扫描程序”,
templateUrl:'./scanner.component.html',
样式URL:['./scanner.component.css']
})
导出类ScannerComponent实现OnInit{
@ViewChild(“视频”)视频:HTMLMEDIALEMENT;
构造函数(){}
恩戈尼尼特(){
这个.cameraCheck();
}
cameraCheck(){
navigator.mediaDevices
.getUserMedia({
视频:{
面向模式:“环境”
}
})
.then(函数(流){
this.video.srcObject=流;
this.video.setAttribute('playsinline','true');//告诉iOS safari我们不想要全屏
这个.video.play();
});
}
}

getUserMedia的承诺句柄中新的
函数(流)
似乎没有获得正确的
引用。因此出现了错误。将其更改为使用箭头函数可以解决此问题

例如:

cameraCheck() {
    navigator.mediaDevices
      .getUserMedia({
        video: {
          facingMode: 'environment'
        }
      })
      .then((stream) => {
        this.video.srcObject = stream;
        this.video.setAttribute('playsinline', 'true'); // required to tell iOS safari we don't want fullscreen
        this.video.play();
      });
  }
}

很乐意帮忙。箭头函数的工作原理类似于闭包,这意味着它们可以记住并访问声明它们的环境。因此,您可以使用箭头功能访问此。