在.ts文件

时间:2018-05-18 10:43:39

标签: javascript angular ionic3 inappbrowser

目前我需要在CheckSocialMedia函数中向浏览器添加一个事件。但是它说无法找到名字。

那么如何制作浏览器' .ts文件中的变量global?我尝试了一些东西,但没有任何工作。感谢

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { InAppBrowser, InAppBrowserOptions } from "@ionic-native/in-app-browser";
import { Platform } from 'ionic-angular';
import { AppAvailability } from '@ionic-native/app-availability';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    public browser: object//doesn't work

  constructor(public navCtrl: NavController, private inAppBrowser: InAppBrowser, public platform: Platform, private appAvailability: AppAvailability)
  {
      //options for iab
      const options: InAppBrowserOptions = 
      {
          toolbar: 'no',
          location: 'no',
          zoom: 'no'
      }

      //when device rdy
     this.platform.ready().then( () => {

         browser = this.inAppBrowser.create("https://www.awebsite.co.uk/", '_blank', options);
            browser.on('loadstart').subscribe(event => { this.CheckSocialMediaLinks(event.url); });
     })

  }

  //check if link is for social media
  CheckSocialMediaLinks(url: string){ //I need to resubscribe the event
      const watch = browser.on('loadstart').subscribe(event => { this.CheckSocialMediaLinks(event.url); });
}

1 个答案:

答案 0 :(得分:1)

总的来说,Globals很糟糕,它在Ionic中是一种反模式,最好的做法是使用provider存储所有全局变量,并在需要这些全局变量的组件中使用此提供程序。 看一下这个讨论,迈克回答。 https://forum.ionicframework.com/t/global-variables-on-ionic-2/45890/7?u=tomstumshais

或者再举一个例子来解决这个全局问题。 https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/5?u=tomstumshais