【问题标题】:Uncaught TypeError: Cannot read property 'isAvailable' of undefined未捕获的类型错误:无法读取未定义的属性“isAvailable”
【发布时间】:2021-08-07 22:42:01
【问题描述】:

我收到以下错误。问题是“这个”的功能。也许你可以帮助我。谢谢;)

StatQuestionnaire.html:5 错误类型错误:无法读取未定义的属性“isAvailable”
在 Channel.webpackJsonp.1020.StatQuestionnaire.onDeviceReady (main.js:1049)
在 Channel.subscribe (cordova.js:772)
在 document.addEventListener (cordova.js:129)
在 HTMLDocument.document.addEventListener (cordova.js:1694)
在 StatQuestionnaire.webpackJsonp.1020.StatQuestionnaire.printStatSite (main.js:1033)
在 Object.eval [as handleEvent] (StatQuestionnaire.html:5)
在handleEvent (vendor.js:13512)
在 callWithDebugContext (vendor.js:14804)
在 Object.debugHandleEvent [as handleEvent] (vendor.js:14392)
在 dispatchEvent (vendor.js:10412)`

  private myPrinter: Printer;
  private browser: boolean = true;
  private content: string;

  constructor(public navCtrl: NavController, public navParam: NavParams, public httpProv: HttpProvider,
              public alertCtrl: AlertController , public loadingCtrl: LoadingController, public printer: Printer) {
    super(httpProv, alertCtrl);
    
    this.currQuest = this.navParam.data.questionnaire;
    this.myPrinter = printer;
   
    this.fullTime = true;
    
    window.onresize = (e) => {
      this.startPlotShowing();
    }
  }
  
  printStatSite() {
    ...
        
        document.addEventListener('deviceready', this.onDeviceReady, false);
  
        if(this.browser) {
           var win = window.open('','','width=3508,height=2480,toolbar=0,scrollbars=0,status=0');
           win.document.write(this.content);
           win.document.close();
           win.print();
        }
     }
  
  onDeviceReady() {
      this.browser = false;
  
      let options: PrintOptions = {
        name: 'Fragebogen Auswertung',
        landscape: true
      }
  
      var isAvailable: boolean = true;
      this.myPrinter.isAvailable().then((result) => {isAvailable = true;}, (result) => {isAvailable = false;});
  
      
      if(isAvailable) {
        this.myPrinter.pick();
        this.myPrinter.print(this.content, options);
        
      } else {
        console.log("Checks whether the device is capable of printing/Checks if the printer service is available (iOS) or if printer services are installed and enabled (Android).");
      }
    }
}

【问题讨论】:

    标签: typescript cordova ionic-framework


    【解决方案1】:

    问题是您的onDeviceReady 函数是用动态this 调用的。您可以通过将 onDeviceReady 定义为类属性来解决此问题:

    
    ...
    onDeviceReady = () => {
      this.browser = false;
    ...
    

    或者在构造函数中直接绑定到对象的this

    constructor(...) {
      super(httpProv, alertCtrl);
    
      ...
    
      this.onDeviceReady = this.onDeviceReady.bind(this);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 2015-01-06
      • 2017-07-26
      • 2019-02-26
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多