javascript - Angular5 Instascan - 打开相机不工作 <视频>

标签 javascript html angular typescript

Angular5 - Instascan - 显示相机 View (视频标签)不起作用。但我的相机是打开的。

<小时/>

app.component.ts

constructor(){    
this.Instascan= require('instascan'); 

let scanner = new this.Instascan.Scanner({ video: document.getElementById("preview") });

scanner.addListener('scan', function (content) {
  console.log(content);
});
this.Instascan.Camera.getCameras().then(function (cameras) {
  if (cameras.length > 0) {
    scanner.start(cameras[0]);
  } else {
    console.error('No cameras found.');
  }
}).catch(function (e) {
  console.error(e);
});
}

app.component.html

<video id="preview"></video>

Instascan --- https://github.com/schmich/instascan

最佳答案

let scanner = new this.Instascan.Scanner({ video: document.getElementById("preview") });

问题是,当 View 尚未初始化时,您正在构造函数中执行上述代码。 document.getElementById("preview") 将返回 null,因为此时没有找到任何元素。因此,您尝试使用它的东西从未正确初始化。

您应该在 Angular 的 AfterViewInit Hook 中运行该代码。另外,您应该使用 ViewChild 装饰器来获取 ElementRef,而不是使用 document。像您在这里所做的那样访问 DOM 是不正确的做法。

关于javascript - Angular5 Instascan - 打开相机不工作 <视频>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48220807/

相关文章:

javascript - jQuery HeapBox "this"事件内部

html - 如何在 HTML 中使用 Font Awesome 图标

javascript - Bootstrap : Stylize Border Lines in Navbar Menu

html - 动态大小的 html 视频,其正下方有元素

html - 如何阻止 md-card 占据整个水平宽度?我如何并排放置它们?

Javascript HTML 包含重复结果包含在随机位置

Javascript 从对象获取键和值

javascript - 如何使用 JavaScript 格式化日期?

json - 从 Angular 2 typescript 中的 json 响应对象获取键/值对

javascript - 无法使用 updateNg1Component() 在 Angular 2 应用程序中使用 Angular 1 指令