Angular 版本:4
从 LoginComponent
登录后(位于 public/signin
路线,我想在路径 TestComponent
导航到 public/test
。
我这样做如下:this._router.navigate(['public/test'])
然后,它会正确重定向,但问题是它不会删除 LoginComponent
来自 DOM(尽管会调用 ngOnDestroy
)。
值得一提的是导航到TestComonent
使用
<a routerLink='/public/test'>Test</a>
按预期工作。
我做错了什么?
应用程序路由.ts:
const APP_ROUTES:Routes = [
{path: 'public', loadChildren: './authentication/authentication.module#AuthenticationModule'}
];
export const APP_ROUTING:ModuleWithProviders = RouterModule.forRoot(
APP_ROUTES, {
useHash: false, enableTracing: true, initialNavigation: true});
` authentication.routing.ts(这两个组件都属于):
const routes: Routes = [
{
path: '',
children: [
{path: 'test', component: TestComponent},
{path: 'signin', component: LoginComponent},
]
}
];
export const routing = RouterModule.forChild(routes);
最佳答案
我也有同样的问题。 我通过在路由之前添加这个来修复:
this.zone.run(() => {
// Your router is here
this._router.navigate(['public/test'])
});
关于Angular Router 在以编程方式导航时,旧组件保留在 dom 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45566128/