TypeError: Cannot read property 'addEventListe

2019-09-10 04:47发布

问题:

I trying to open in my app remote address using cordova pluguin InAppBrowser. I use that code:

import { Injectable } from "@angular/core";
import { HttpQueryService } from "./httpqueryservice";
import { ToastController } from "ionic-angular";
import { InAppBrowser, InAppBrowserEvent } from "ionic-native";

/**
 * Класс сервиса для открытия окон в браузере
 */
@Injectable()
export class WindowOpenService {

  inAppBrowserRef: InAppBrowser; //объект с браузером

  /**
   * Конструктор класса
   */
  constructor(
    public toastCtrl: ToastController,
    public httpQueryService: HttpQueryService
  ){

  }

  /**
   * Open url in InAppBrowser
   *
   * @param url
   * @param target
   * @param options
   */
  open(url = 'http://192.168.1.2/myurl/', target = '_blank', options = 'location=yes,hidden=no,hardwareback=yes,toolbar=yes')
  {
    try{
      var inAppBrowserRef = new InAppBrowser(url, target, options);
      inAppBrowserRef.on('loadstart').subscribe((result) => {
        alert(result);
        inAppBrowserRef.close();
      });
      inAppBrowserRef.on('mychange').subscribe((result) => {
        alert(result);
        this.inAppBrowserRef.close();
      });
      inAppBrowserRef.on('loadstop').subscribe((result) => {
        alert(result);
        this.inAppBrowserRef.close();
      });
      inAppBrowserRef.on('loaderror').subscribe((result) => {
        alert(result);
        inAppBrowserRef.close();
      });
    }catch(e){
      alert(e);
    }
  }
}

And I get an Error on 'loadstart' event: TypeError: Cannot read property 'addEventListener' on undefined

I think that all is allright. But it fire the error? What I'm doing wrong? I use this pluguin https://ionicframework.com/docs/v2/native/inappbrowser/

And test it on emulator and on device with Android 6 version. The same problem. In config.xml I use that code:

<allow-navigation href="*://192.168.1.2/*"/>

And I use the lastest Ionic 2.2.1 version.

回答1:

The problem was that I'm do not install pluguin before use the class.

ionic plugin add cordova-plugin-inappbrowser