Angular-尝试使用深层链接,但仅打开应用程序,路由不起作用

时间:2018-10-04 14:06:39

标签: angular ionic-framework ionic3 cordova-plugins ionic-native

我正在尝试使用深层链接,但只有该应用程序正在打开。路由不起作用。

这是我的代码:

import { Component, ViewChild } from ‘@angular/core’;
import { App, Platform, Nav, NavController } from ‘ionic-angular’;
import { StatusBar } from ‘@ionic-native/status-bar’;
import { SplashScreen } from ‘@ionic-native/splash-screen’;
import { Deeplinks } from ‘@ionic-native/deeplinks’;
import { HomePage } from ‘…/pages/home/home’;
import { HarryPage } from ‘…/pages/harry/harry’;

@Component({
    templateUrl: ‘app.html’
})

export class MyApp {
    @ViewChild(‘myNav’) nav: Nav;
    rootPage:any = HomePage;

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private deeplinks: Deeplinks) {
        platform.ready().then(() => {
            statusBar.styleDefault();
            splashScreen.hide();
        });
        this.deeplinks.routeWithNavController( this.nav, {
            ‘/harry’: HarryPage,
            ‘/’ :HomePage
        }).subscribe( (match)=>{
                console.log(match.$route);
                // alert(JSON.stringify(match))
            }, (noMatch)=>{
                // alert(JSON.stringify(noMatch));
            })
    }
}

每当我尝试访问/ harry路线时,只有主页打开。

2 个答案:

答案 0 :(得分:0)

尝试在deeplinks回调中使用platform.ready()逻辑。

      @ViewChild(Nav) nav:Nav;

      constructor(private deeplinks: Deeplinks) {

        platform.ready().then(() => {

         this.deeplinks.routeWithNavController(this.nav, {
            '/harry': HarryPage,
            '/' : HomePage

          }).subscribe(match => {

            console.log('Successfully matched route', JSON.stringify(match, null, 2));
          }, nomatch => {

            console.error('Got a deeplink that didn\'t match', nomatch);
          });
        });  
 }

答案 1 :(得分:0)

如果您在根页面中放置相同的代码,则可以正常工作;如果遵循他们的文档,则无济于事。