未捕获(承诺):TypeError:无法读取属性' init'未定义的

时间:2017-07-19 08:39:27

标签: javascript angular typescript ionic-framework typeerror

我试图让QuggaJS在我的Ionic3 / Angular4 App中运行。 但我总是得到错误:

Uncaught (in promise): TypeError: Cannot read property 'init' of undefined TypeError: Cannot read property 'init' of undefined at new Scanner 

我真的不知道为什么Quarra未定义,因为我通常将它导入我的项目? 首先我做了:

npm install quagga

之后,它确实在我的项目中导入了它,如下所示:

import * as Quagga from 'quagga';

但我仍然收到错误。

我的Typescript代码如下所示:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import * as Quagga from 'quagga';

@Component({
  selector: 'page-scanner',
  templateUrl: 'scanner.html',
})
export class Scanner {

  constructor(public navCtrl: NavController,
    public navParams: NavParams) {
      Quagga.init({
          inputStream : {
              name : "Live",
              type : "LiveStream",
               // Or '#yourElement' (optional)
              target: document.querySelector('#scanner')
          },
          decoder : {
           //Change Reader for the right Codes
           readers: [ "code_128_reader",
                      "ean_reader",
                      "ean_8_reader",
                      "code_39_reader",
                      "code_39_vin_reader",
                      "codabar_reader",
                      "upc_reader",
                      "upc_e_reader",
                      "i2of5_reader" ],
          }
        }, function(err) {
            if (err) {
                console.log(err);
                return
            }
            console.log("Initialization finished. Ready to start");
            Quagga.start();
        });
  }
}

我真的希望你们能解决这个问题!谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

如果您使用的是Angular-CLI,请将此添加到您的scripts数组中(对于webpack也是如此):

"scripts": [
        "node_modules/path/qugga.min.js"
      ],

添加

declare var Quagga:any;

导致:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import * as Quagga from 'quagga';

@Component({
  selector: 'page-scanner',
  templateUrl: 'scanner.html',
})
export class Scanner {

  constructor(public navCtrl: NavController,
    public navParams: NavParams) {}

  ngOnInit() {
      Quagga.init({

此外,如果此操作无效,请将Quagga.init({移至ngOnInit(),以确保该组件已完全加载。