3

I'm trying to use QuaggaJS with Angular 2. I have the quagga.d.ts file in the app folder and the following import statements in the component:

import Quagga from './quagga.d';

The guide says from 'quagga' but it doesn't work but above works

declare const Quagga = require('quagga').default;

I have the following code in the constructor of the component like this:

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();
  });


 }

However, I get the following error:

Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'init' of undefined from core.umd.js

and

 Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'init' of undefined
TypeError: Cannot read property 'init' of undefined

from zone.js

What does this error mean? I can't figure this out! Is there a bug with zone.js?

Any help would be greatly appreciated!

4

1 回答 1

5

添加 Quagganode_modules运行

npm install quagga --save

例如,像往常一样添加jscss依赖项index.html

<script src="node_modules/....../qugga.min.js"></script>

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();
      });
  }

}

使用ngOnInit而不是构造函数。而构造函数只实例化组件,ngOnInit会在组件加载后调用。

此外,请查看 https://github.com/serratus/quaggaJS/issues/146以获得您在下一步中需要的其他帮助。

于 2017-03-06T11:21:17.893 回答