Vin*_*nce 5 angular2-routing angular
试图在我的 angular2 应用程序中创建我的重定向路由。
但我的问题是,当有人输入像“nopath”这样的无效路径时,用户会被重定向到“HomeComponent”,但 url 仍然保留“/#/nopath”
我也希望 redirectTo 路由更改 url!我应该如何实现这一目标?
我应该在我的 HomeComponent 构造函数中放置一个 if 来检查当前 url 并将他的路由更改为 homecomponent 吗?还是我遗漏了什么?
路线:
const routes: Routes = [
{ path: '', pathMatch: 'full', component: HomeComponent },
{ path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] },
{ path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] },
{ path: 'users', component: UserComponent, canActivate: [AuthGuard] },
{ path: '**', redirectTo: '' }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes, {useHash: true});
Run Code Online (Sandbox Code Playgroud)
编辑:
试过了,但我没有重定向到 home 组件
const routes: Routes = [
{ path: '', pathMatch: 'full' , redirectTo: 'home' },
{ path: 'home', component: HomeComponent },
{ path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] },
{ path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] },
{ path: 'users', component: UserComponent, canActivate: [AuthGuard] },
{ path: '**', redirectTo: '' }
];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8039 次 |
| 最近记录: |