我只是在学习本族语,棱角形和打字稿。我正在使用模拟器为iOS开发。我的路由配置如下(从NativeScript检查器复制粘贴):
config: Array (7)
0{path: "filtered/:topic/:queryText", component: function}
1{path: "abstract/:id", component: function}
2{path: "session/:id", component: function}
3{path: "details/:id", component: function}
4{path: "home", component: function}
5{path: "", redirectTo: "home", pathMatch: "full"}
6{path: "**", redirectTo: "home"}从home到details的路由使用home.component中的以下代码
showDetails(e){
console.log(e.index);
this.router.navigate(["details", e.index]);
}这在大多数情况下都能工作,但当我第一次导航到filtered组件,然后使用Back按钮返回时,就会可靠地失败。showDetails函数会触发,记录e.index的正确值,但是应用程序不会路由到details;home页面根本不会改变(但我仍然可以从home页面导航到其他页面)。
我的问题是,我应该如何解决这个问题?我可以在检查器中看到this.router对象,但是它是一个非常复杂的对象,看起来有数百个嵌套属性。我猜我应该尝试找出this.router在showDetails函数按预期工作时的样子,以及当showDetails不再工作时它是如何变化的。但是我应该关注哪些属性呢?还是我应该看别的东西?
非常感谢您提供的任何建议或指导。
更新:当我导航到其他两个以上的页面时,问题就会发生,例如,如果我只导航到filtered,它就不会发生,但是如果从filtered导航到abstract,然后使用back按钮两次,它总是会发生。如果首先导航到session,然后导航到abstract,然后再导航到home,则相同。
更新2:这里的答案对于调试路由非常有帮助:
How to detect a route change in Angular 2?
更新3:我想明白了。我的路线中的一个参数有时是空的,导致一个带有双斜杠的url:/filtered//search-term。看起来不太喜欢这样。
发布于 2019-07-18 10:07:50
试试这条路的路线。
应用程序-routing.module.ts :-
{ path: "", redirectTo: "/home", pathMatch: "full" },
{ path: "home", loadChildren: "~/app/home/home.module#HomeModule" },
{ path: "details/:id", loadChildren: "~/app/details/details.module#DetailsModule" },
{ path: "session/:id", loadChildren: "~/app/session/session.module#SessionModule" }重定向到页面的 :-
import { NgZone } from "@angular/core";
import { isIOS } from "tns-core-modules/platform";
import { RouterExtensions } from "nativescript-angular/router";
constructor(private zone: NgZone,
private _routerExtensions: RouterExtensions){ }
gotoStartPage() {
setTimeout(() => {
this.zone.run(() => {
this._routerExtensions.navigate(["details", 2], {
clearHistory: (isIOS) ? true : false,
});
});
}, 500);
}https://stackoverflow.com/questions/40388709
复制相似问题