试图在我的angular2应用中创建我的重定向路由。Angular2重定向路由不会改变浏览器网址
但我的问题是,当有人输入了“NOPATH”,那么用户会被重定向到“HomeComponent”,但网址仍然保持“/#/ NOPATH”
路径无效,我想redirectTo路线也更改网址! 我应该如何实现这一目标?
我应该把一个如果在我的HomeComponent构造函数,检查当前的网址,并改变他的路线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});
编辑:
想这一点,但我没有得到重定向到主成分
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: '' }
];
也许不使用重定向路由,只需使用“**”,组件:HomeComponent(而不是重定向)。 (NM尝试了几种方法,是的,我不会这样做)。 –
你使用的是最新的Angular2和路由器版本吗?最近我看到了类似的问题,在更新解决它的地方。 –
我使用3.2.3版本的路由器,我不知道如何检查我是否有最新版本 – Vince