QuaggaJS with Angular 2 problems

匆匆过客 提交于 2019-12-02 03:16:03

问题


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!


回答1:


To add Quagga to node_modules run

npm install quagga --save

add js and css dependencies in index.html as usual for example

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

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

}

Use ngOnInit instead of the constructor. While the constructor only instantiates the component, ngOnInit will be called after the component is loaded.

Also, take a look at https://github.com/serratus/quaggaJS/issues/146 for some other help you would need in next step.



来源:https://stackoverflow.com/questions/42555644/quaggajs-with-angular-2-problems

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!