gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:13:13 24 4
gpt4 key购买 nike

当从一个子模块内从一个子路由导航到另一个同级子路由时,路由器不会破坏前一个组件,而是在向前和向后的导航中附加新的组件。

为什么会这样?

/#/subscriber/lookup开始,移动到/#/subscriber/register路由

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

应用程序路由.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 }
}]

},];

应用程序模块.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]
})

订阅者.module.ts

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

这是导航时发生的事情:

appending routes

最佳答案

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

在我的例子中,在模板中我有 {{model.field}} 并且在 TypeScript 中有 model: Model;,但是当创建组件时, 模型未定义。我认为如果在创建组件时抛出任何异常,则路由器无法销毁旧组件。

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

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

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

关于Angular 4 路由器在 routerLink 导航上附加组件而不是销毁它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com