对我来说,奇怪的是,唯一可行的方法是使用 routeWithNavController 方法并在第二个参数上传递一个空对象。顺便说一句,我正在使用 Ionic 3 并在 iPhone 模拟器上进行测试。
这里是测试:
/**
* Using the routeWithNavController method and passing an empty object on the second parameter
* WORKS!!
* Did open the app and fired the "match" callback with the info of the clicked link
*/
this.platform.ready().then(() => {
this.deeplinks.routeWithNavController(this.nav, {
// Nothing here. Empty object
}).subscribe((match) => {
alert('Match');
alert(JSON.stringify(match));
}, (nomatch) => {
alert('No Match');
alert(JSON.stringify(nomatch));
});
});
以下示例无效。
/**
* Using the routeWithNavController method and passing an object with a "what would be" route on the second parameter
* DOESN`T WORK
* Did open the app but didn't fire any of the callbacks. Neither "match" or "nomatch"
*/
this.platform.ready().then(() => {
this.deeplinks.routeWithNavController(this.nav, {
'/': {}
}).subscribe((match) => {
alert('Match');
alert(JSON.stringify(match));
}, (nomatch) => {
alert('No Match');
alert(JSON.stringify(nomatch));
});
});
/**
* Using the route method and passing an object empty object
* DOESN'T WORK (kinda)
* Did open the app and fired the nomatch callback (after all, no route specified)
*/
this.platform.ready().then(() => {
this.deeplinks.route({
// Nothing here. Empty object,
}).subscribe((match) => {
alert('Match');
alert(JSON.stringify(match));
},
(nomatch) => {
alert('No Match');
alert(JSON.stringify(nomatch));
});
});
/**
* Using the route method and passing object with a "what would be" route on the second parameter
* By the way, here I tried both links:
* myapp://myapp.domain.com/
* myapp://myapp.domain.com
* DOESN`T WORK
* Did open the app but didn't fire any of the callbacks. Neither "match" or "nomatch"
*/
this.platform.ready().then(() => {
this.deeplinks.route({
'/': {},
}).subscribe((match) => {
alert('Match');
alert(JSON.stringify(match));
},
(nomatch) => {
alert('No Match');
alert(JSON.stringify(nomatch));
});
});
因为只有第一个有效,我不得不自己处理匹配对象数据并做路由,像这样:
this.deeplinks.routeWithNavController(this.nav, {}).subscribe((match) => {
if (match.$link.path == "/list") {
this.nav.setRoot(ListPage);
}
}, (nomatch) => {});
这是我的工作 app.component.ts 的完整代码,带有深层链接:
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { Deeplinks } from '@ionic-native/deeplinks';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = HomePage;
pages: Array<{title: string, component: any}>;
constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public deeplinks: Deeplinks) {
this.initializeApp();
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: HomePage },
{ title: 'List', component: ListPage }
];
}
initializeApp() {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.statusBar.styleDefault();
this.splashScreen.hide();
this.deeplinks.routeWithNavController(this.nav, {}).subscribe((match) => {
if (match.$link.path == "/list") {
this.nav.setRoot(ListPage);
}
}, (nomatch) => {});
});
}
openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
}