我正在尝试在 Angular 2 中使用 QuaggaJS。我在 app 文件夹中有 quagga.d.ts 文件,在组件中有以下导入语句:
import Quagga from './quagga.d';
Run Code Online (Sandbox Code Playgroud)
指南从“quagga”说,但它不起作用,但上面起作用
declare const Quagga = require('quagga').default;
Run Code Online (Sandbox Code Playgroud)
我在组件的构造函数中有以下代码,如下所示:
constructor() {
Quagga.init({
inputStream : {
name : "Live",
type : "LiveStream",
target: document.querySelector('#yourElement') // Or '#yourElement' (optional)
},
decoder : {
readers : ["code_128_reader"]
}
}, function(err) {
if (err) {
console.log(err);
return
}
console.log("Initialization finished. Ready to start");
Quagga.start();
});
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'init' of undefined 来自 core.umd.js
和
Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'init' of undefined
TypeError: Cannot read property 'init' of undefined
Run Code Online (Sandbox Code Playgroud)
来自 zone.js
这个错误是什么意思?我想不通!zone.js 有错误吗?
任何帮助将不胜感激!
添加 Quagganode_modules运行
npm install quagga --save
Run Code Online (Sandbox Code Playgroud)
例如,像往常一样添加js和css依赖项index.html
<script src="node_modules/....../qugga.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
在 app.component.ts
import { Component, OnInit } from '@angular/core';
declare var Quagga:any;
@Component({
selector: 'app-root',
template: `<router-outlet></router-outlet>`
})
export class AppComponent implements OnInit {
ngOnInit() {
Quagga.init({
inputStream : {
name : "Live",
type : "LiveStream",
target: document.querySelector('#yourElement') // Or '#yourElement' (optional)
},
decoder : {
readers : ["code_128_reader"]
}
}, function(err) {
if (err) {
console.log(err);
return
}
console.log("Initialization finished. Ready to start");
Quagga.start();
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用ngOnInit代替构造函数。虽然构造函数只实例化组件,但ngOnInit会在组件加载后调用。
另外,请查看 https://github.com/serratus/quaggaJS/issues/146以获取下一步所需的其他帮助。
| 归档时间: |
|
| 查看次数: |
3569 次 |
| 最近记录: |