angular - Video in Angular - TypeError Issue
问题描述
I'm trying to do some basic video stream work in Angular. My code is below. Challenge is I keep getting and error that says... ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'video' of undefined TypeError: Cannot read property 'video' of undefined
Any suggestions would be appreciated.
import {
Component,
OnInit,
ViewChild,
ElementRef
} from '@angular/core';
@Component({
selector: 'app-scanner',
templateUrl: './scanner.component.html',
styleUrls: ['./scanner.component.css']
})
export class ScannerComponent implements OnInit {
@ViewChild('video') video: HTMLMediaElement;
constructor() {}
ngOnInit() {
this.cameraCheck();
}
cameraCheck() {
navigator.mediaDevices
.getUserMedia({
video: {
facingMode: 'environment'
}
})
.then(function(stream) {
this.video.srcObject = stream;
this.video.setAttribute('playsinline', 'true'); // required to tell iOS safari we don't want fullscreen
this.video.play();
});
}
}
<div>
<video #video></video>
</div>
解决方案
function(stream)
承诺句柄中的 newgetUserMedia
似乎没有得到正确的this
参考。因此错误。将其更改为使用箭头功能应该可以解决问题。
例子:
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();
});
}
}
推荐阅读
- swift - 我可以将按钮放到单独的视图中吗?
- sql - 在返回多个表的表中的每一列上选择不同的
- postgresql - 错误:json 类型的输入语法无效
- javascript - 我怎样才能使按钮的行为像后退箭头键一样?
- ios - Apple 审核流程的应用程序配置
- c# - C# 多行文本框到 .TXT 文件
- c# - 将上传的 .csv 转换为 memorystream,然后转换为 .Net Core 中的 c# 数据表
- javascript - 嵌套 for 循环与从 Nodejs 中的 API 获取数据相结合?
- ios - 崩溃:com.apple.CFNetwork.LoaderQ SIGABRT ABORT
- javascript - Sequelize 在使用 .then() 查询后无法分配数据