QuaggaJS with Angular 2 problems

后端 未结 1 360
情话喂你
情话喂你 2021-01-20 21:51

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         


        
相关标签:
1条回答
  • 2021-01-20 21:58

    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.

    0 讨论(0)
提交回复
热议问题