如何解决Angular 路由器重定向到相同的路由只是在最后添加登录
此错误 Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'login/home'
。
基本上是我的问题,我希望它重定向到 http://localhost:4200/home not http://localhost:4200/login/home
这是我的路由模块
{ path:'',component: HomeComponent,canActivate:[AuthGuardService] },{ path:'home',{ path:'login',component: LoginComponent },{ path:'register',component: RegisterComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],exports: [RouterModule]
})
and this is the calling code this.router.navigate([''])
解决方法
尝试使用不同的 router-outlet
标记将登录组件和任何公共组件与 home 组件分开,如下所示:
app.component.html
<router-outlet></router-outlet>
home.component.html
<router-outlet></router-outlet>
以及您的路由器模块和路由项目,如下所示:
{ path:'login',component: LoginComponent },{ path:'register',component: RegisterComponent },{ path:'',redirectTo: '/home',pathMatch: 'full' },{ path:'home',component: HomeComponent,canActivate:[AuthGuardService],children: [
{path: 'child-a',component: ChildAComponent
}
]
},{ path: '**',component: PageNotFoundComponent },// Wildcard route for a 404 page
然后你可以使用这些路由器链接:
http://localhost:4200/login
http://localhost:4200/register
http://localhost:4200/home
http://localhost:4200/home/child-a
像这样:
this.router.navigate(['/login']);
this.router.navigate(['/register']);
this.router.navigate(['/home']);
this.router.navigate(['/home/child-a']);
this.router.navigate(['/']);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。