Angular 4路由器在routerLink导航上附加组件而不是销毁它们

时间:2017-08-18 18:11:52

标签: angular angular-routing angular-router routerlink

当从子路径中的子模块导航到另一个兄弟路径时,而不是路由器销毁前一个组件,它会向前和向后追加新的路径。

为什么会这样?

/#/subscriber/lookup开始,转到/#/subscriber/register路线

<a [routerLink]="['../register']">Subscriber register link</a>

app.routes.ts

/**
 * Angular 2 decorators and services
 */

import { Routes } from '@angular/router';

/**
 * Other services
 */
import { RouteProtection } from '../services/route-protection.service';
// import { DataResolver } from './app.resolver';

/**
 * Imported Components
 */
import { LoginComponent } from '../login/login.component';
import { NotFound404Component } from '../404/notfound404.component';

export const ROUTES: Routes = [{
   path: '',
   redirectTo: 'subscriber',
   pathMatch: 'full',
}, {
   path: 'subscriber',
   loadChildren: '../+subscriber/subscriber.module#SubscriberModule',
   // canActivate: [RouteProtection]
}, {
   path: 'detail',
   loadChildren: '../+detail/detail.module#DetailModule',
   canActivate: [RouteProtection]
}, {
   path: 'login',
   component: LoginComponent
}, {
   path: '**',
   component: NotFound404Component
}, {
   path: '404',
   component: NotFound404Component
}];

// export const routing = RouterModule.forRoot(ROUTES, { useHash: true});

subscriber.routes.ts

/**
 * Imported Components
 */
import { SubscriberLookupComponent } from './lookup/subscriber-lookup.component';
import { SubscriberRegisterComponent } from './register/subscriber-register.component';

/*
 * Shared Utilities & Other Services
 */
// import { RouteProtection } from '../services/route-protection.service';
// import { DataResolver } from '../services/app.resolver';

export const subscriberRoutes = [{
   path: '',
   children: [{
      path: '',
      pathMatch: 'full',
      redirectTo: 'lookup'
   }, {
      path: 'lookup',
      component: SubscriberLookupComponent, //canActivate: [RouteProtection],
   }, {
      path: 'register',
      component: SubscriberRegisterComponent, //canActivate: [RouteProtection],  // resolve: {      'dataBroughtToComponent': DataResolver   }
   }]

},];

app.module.ts

/**
 * `AppModule` is the main entry point into Angular2's bootstraping process
 */
@NgModule({
   bootstrap: [AppComponent],
   declarations: [ // declarations contains: components, directives and pipes

      // Components
      AppComponent, LoginComponent, NotFound404Component, // Directives
      NavSidebarComponent, NavHeaderComponent, NavFooterComponent

      // Pipes

   ],
   imports: [ // import other modules
      BrowserModule, SharedModule, BrowserAnimationsModule, RouterModule.forRoot(ROUTES, {useHash: true}), NgbModule.forRoot()
      /*      ApplicationInsightsModule.forRoot({
               instrumentationKey: '116b16e7-0307-4d62-b201-db3ea88a32c7'
            })*/

   ],
   providers: [ // expose our Services and Providers into Angular's dependency injection
      ENV_PROVIDERS, APP_PROVIDERS, AUTH_PROVIDERS]
})

subscriber.module.ts

@NgModule({
   imports: [
      SharedModule,
      CommonModule,
      RouterModule.forChild(subscriberRoutes)
   ],
   declarations: [ // Components / Directives / Pipes
      SubscriberLookupComponent,
      SubscriberRegisterComponent
   ],
   // exports: [
   //    SharedModule,
   //    SubscriberLookupComponent,
   //    SubscriberRegisterComponent
   // ]
})

导航时会发生这种情况:

appending routes

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题。我发现当我的新组件被创建时,它会抛出异常,然后我的旧组件没有被销毁。

就我而言,在模板中我有{{model.field}}和TypeScript model: Model;,但在创建组件时,modelundefined。我认为如果组件的创建有任何异常,路由器就不能破坏旧组件。

我使用Visual Studio Code调试器来查找异常原因。

所以我只是推了一个*ngIf来检查模型:

<tag *ngIf="model">
    {{model.field}}
</tag>

答案 1 :(得分:2)

起初我以为这是因为我通过 BrowserSync 查看我的角度应用。

似乎出于某种原因,BrowserSync的javascript与角落的路由器相混淆。我假设这是一个错误。当我直接查看我的应用程序时,路由器按预期工作。

但是,它在生产中再次发生,没有browserync。

Caio Filipe的回答为我解决了这个问题。 这是因为我的组件A引发了错误,因此在导航到组件B时,由于错误导致组件A无法销毁。我submitted a ticket有角度。