2017-08-08 31 views
4

角版本:4角路由器,旧组件在DOM停留

LoginComponent(位于public/signin航线登录后,我想导航到TestComponent路径public/test 我。这样做如下:this._router.navigate(['public/test']) 然后,它正确地重定向,但问题是,它并没有从DOM删除LoginComponent(虽然ngOnDestroy不会被调用)

值得导航到TestComonent使用提。<a routerLink='/public/test'>Test</a>按预期工作。

我在做什么错?

app.routing.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); 
+0

我有同样的确切的问题,你有没有想出如何解决呢? thx。 –

+0

你有一个正常的Angular设置吗?或者它是一个混合应用程序? – Ben

+0

我有一个正常的设置... –

回答

0

我有同样的问题,因为你发现一(临时)解决。 首先,我认为这是一个模块上延迟加载的路由问题,我改为使用组件,但问题从未消失。

最后我在我的组件中实现了OnDestroy(来自核心)。 当ngOnDestroy函数被调用时,我写了一个小的javascript,从DOM中删除静止元素。

var parent = document.getElementsByTagName("parent-name"); 
var child = document.getElementsByTagName("ng-component"); 
parent[0].removeChild(child[0]); 

我知道这只是一个临时工作,但也许够好,直到你从某人那里得到最终答案。

希望

+0

感谢您的意见。你有什么特别的配置给你的项目,或者它是一个干净的角度cli吗? – Ben

0

你有没有尝试添加斜线的开始,使之正好你routerlink匹配?

this._router.navigate(['/public/test']) 

这里是在路线上相对VS绝对路径的详细信息:https://angular.io/guide/router#relative-navigation

+0

不幸的是,它仍然无法正常工作。我也尝试了'navigateToUrl'和'relativeTo',但它们都不起作用。 – Ben

+0

你可以建立一个演示问题的重拳吗? – DeborahK

+0

我只在使用混合应用程序(ng1 + ng2应用程序)时遇到此问题。我并不确定如何将这样的应用上传到盗取者。此外,只有在ng2应用程序,它按预期工作。有任何想法吗? – Ben

1

我有同样的问题。 我固定的路由前补充一点:

this.zone.run(() => { 
        // Your router is here 
        this._router.navigate(['public/test']) 
       });