TypeError: Cannot read property 'addEventListener' on undefined Angular2, Ionic 2, InAppBrowser

断了今生、忘了曾经 提交于 2019-12-12 03:56:55

问题


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


来源:https://stackoverflow.com/questions/42092228/typeerror-cannot-read-property-addeventlistener-on-undefined-angular2-ionic

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