2017-02-12 224 views
4

取得網址。每次是undefined。我錯過了什麼?不能與深層鏈接

我使用Ionic 2與Ionic Deeplinks插件。在這裏,我正在使用的代碼:

app.component.ts

import { Component, ViewChild } from '@angular/core'; 
    import { App, Platform, AlertController } from 'ionic-angular'; 
    import { StatusBar } from 'ionic-native'; 
    import { TabsPage } from '../pages/tabs/tabs'; 
    import { NavController, Nav } from 'ionic-angular'; 

    import { LoginPage } from '../pages/login/login'; 
    import { SignupPage } from '../pages/signup/signup'; 
    import { AuthService } from '../pages/login/authservice'; 
    import { HomePage } from '../pages/home/home'; 

    import {Network} from 'ionic-native'; 
    import {Deeplinks} from 'ionic-native'; 

    @Component({ 
     template: `<ion-nav [root]="rootPage"></ion-nav>` 
    }) 
    export class MyApp { 

     rootPage: any = HomePage; 
     @ViewChild(Nav) navChild:Nav; 

     constructor(private platform: Platform, private alert :AlertController) { 
     platform.ready().then(() => { 
      StatusBar.styleDefault(); 

     }); 

     document.addEventListener('deviceready', function() { 
     var notificationOpenedCallback = function(jsonData) { 
     console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData)); 
     if (jsonData.additionalData) { 
     if (jsonData.additionalData.myappurl) 
      this.rootPage = jsonData.additionalData.myappurl; 
     } 

     }; 

    }); 
     } 

     ngAfterViewInit() { 
     this.platform.ready().then(() => { 
     if((window.localStorage.getItem('email') === "undefined" || window.localStorage.getItem('email') === null) || 
      (window.localStorage.getItem('password') === "undefined" || window.localStorage.getItem('password') === null)) { 
      this.rootPage = LoginPage; 
     } else { 
      Deeplinks.routeWithNavController(this.navChild, { 
      '/:urlAfterDomain': HomePage 
      }); 
     } 
     }); 
     } 

    } 

home.ts

import { Component } from '@angular/core'; 

    import { NavController, NavParams } from 'ionic-angular'; 
    import { AuthService } from '../login/authservice'; 
    import {Deeplinks} from 'ionic-native'; 

    @Component({ 
     selector: 'page-home', 
     templateUrl: 'home.html' 
    }) 

    export class HomePage { 
     urlAfterDomain: string; 
     constructor(public navCtrl: NavController, private _params: NavParams) { 

      this.navCtrl = navCtrl; 
      this.urlAfterDomain = _params.get('urlAfterDomain'); 
      alert(this.urlAfterDomain); 
      if (this.urlAfterDomain == null || this.urlAfterDomain === "undefined") { 
      this.urlAfterDomain = "App"; 
      } 
      else { 
      this.urlAfterDomain.replace("myapp_url_scheme://", ""); 
      } 
      this.urlAfterDomain = "https://myapp.com/" + this.urlAfterDomain; 
      alert(this.urlAfterDomain); 
     } 

    } 

home.html的

<ion-header> 
    </ion-header> 
    <ion-content style="overflow: hidden;"> 
     <iframe src={{urlAfterDomain}} height="100%" width="100%" frameborder="0"></iframe> 
    </ion-content> 

回答

2

我會推薦這樣的事情

讓您的網址是這樣的:

<a href="myapp_url_scheme:///?restURL=rest_of_address">Url Scheme Test</a> 

然後你就可以獲取從數據的密鑰「restURL」,它就會開始工作。

+0

我認爲這並不能完全解決我的問題,因爲我認爲使用Ionic Deeplinks Plugin將https:/// rest_of_address轉換爲myapp_url_scheme:///?restURL = rest_of_address會出現問題。但我會做一些測試。謝謝! –

0

請使用此代碼來獲取URL:

Deeplinks.routeWithNavController(this.navChild, { 
     '/:urlAfterDomain': HomePage 
     }).subscribe((match) => { 
     console.log(match); 
     },(nomatch) => { 
     console.log(nomatch); 
    }); 

我相信這會幫助你。