[英]Angular routing not working, it redirects me to the base url
我正在尝试使用angular router
从我的页面导航。
我单击一个button
,然后导航到其他页面。
我当前的网址是: http://localhost:4200/user/12345567
。 从这里,我单击按钮以导航
这是我的button
代码:
leaveArea(){
this.router.navigate([ 'account', 'dashboard'], {relativeTo: this.route});
}
我想在单击时导航到http://localhost:4200/account/dashboard
。
但是相反,我导航到http://localhost:4200
。 有一瞬间(可能是一毫秒或更短的时间),在导航到http://localhost:4200
之前,我确实看到该URL为http://localhost:4200/account/dashboard
。 路由逻辑有问题吗?
编辑
我正在添加routeTrace
结果:
编辑
这是我的路由文件:
const appRoutes: Routes = [
{ path: "", component: HomeComponent },
{ path: "account/dashboard", component: AccountDashboardComponent},
{
path: "user/:id", component: UserComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(appRoutes, { enableTracing: true })],
exports: [RouterModule]
})
export class AppRoutingModule {}
只需尝试像这样使用router.navigateByUrl
leaveArea(){
this.router.navigateByUrl('/account/dashboard');
}
PS:通过在字符串的开头添加/
,将考虑从根级别进行路由。
首先 ,您有空白的路线。
它必须是最后的 。
路由在配置中的顺序很重要,这是设计使然。 路由器在匹配路由时会使用“先赢”策略,因此应将更具体的路由放在不那么具体的路由之上。 ( https://angular.io/guide/router )
所以...
const appRoutes: Routes = [
{ path: "account/dashboard", component: AccountDashboardComponent},
{ path: "user/:id", component: UserComponent},
{ path: "", component: HomeComponent}
];
请尝试将您的路线更改为
const appRoutes: Routes = [
{ path: "", redirectTo: "home", pathMatch: "full" }
{ path: "home", component: HomeComponent },
{ path: "account/dashboard", component: AccountDashboardComponent},
{ path: "user/:id", component: UserComponent }
];
并将您的导航线更改为
this.router.navigate(['/account', 'dashboard']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.