我有一个组件ViewBotsComponent
,它有2个子组件:ViewCodeBasedBotComponent
和ViewPipelineBasedBotsComponent
。
我将其路由如下:
const routes: Route[] = [
{
path: "core", component: CoreWrapperComponent, children:[
{path: 'viewbots', component: ViewBotsComponent, children:
[
{path: 'codebased', component: ViewCodeBasedBotComponent},
{path: 'intelligent', component: ViewPipelineBasedBotsComponent},
// {path: '', component: ViewCodeBasedBotComponent}
]
}
},
...
]
当我在http://localhost:4200/core/viewbots/codebased
和http://localhost:4200/core/viewbots/intelligent
之间切换时,正按预期为子组件调用constructor
和ngOnDestroy
,但没有ngOnit
被称为。
现在变得很奇怪:如果重新加载浏览器标签,一切正常,
预期会调用ngOnInit。
以下是30秒的问题视频,请注意,最初未记录ngOninit,但重新加载后正在记录它: https://youtu.be/_HCh3eJAX9I
以下是我的代码:
父组件html:(view-bots.component.html)
<ul class="nav nav-tabs my-2">
<li class="nav-item">
<a class="nav-link" [ngClass]="{'tab-active':activeTab==='codebased'}" (click)="changeActiveTab('codebased')" ><strong>CODE BASED BOTS</strong></a>
</li>
<li class="nav-item">
<a class="nav-link" [ngClass]="{'tab-active':activeTab==='intelligent'}" (click)="changeActiveTab('intelligent')" ><strong>PIPELINE BASED BOTS</strong></a>
</li>
</ul>
<router-outlet></router-outlet>
父组件ts:(view-bots.component.ts)
export class ViewBotsComponent implements OnInit {
botList$: Observable<IBot[]>;
activeTab:string = "codebased";
constructor(
private serverService: ServerService,
private constantsService: ConstantsService,
private router:Router,
private store: Store) {
}
ngOnInit() {
console.log('ViewBotsComponent init');
}
changeActiveTab(activeTab){
console.log("hi");
this.router.navigate(['core','viewbots',activeTab]);
this.activeTab =activeTab
}
}
Child1 html:view-pipeline-based-bots.component.html
<p>pipelineBasedBotList</p>
Child1 ts:view-pipeline-based-bots.component.ts
export class ViewPipelineBasedBotsComponent implements OnInit, OnDestroy {
ngOnDestroy(): void {
console.log('pipelineBasedBotList destroyed');
}
constructor() {
console.log("ViewPipelineBasedBotsComponent constructor")
}
ngOnInit() {
console.log('pipelineBasedBotList init');
}
}
Child2 html:基于视图代码的bot.component.html
<p>ViewCodeBasedBotComponent</p>
Child2 ts:基于视图代码的bot.component.ts
export class ViewCodeBasedBotComponent implements OnInit, OnDestroy {
constructor(private store: Store) {
console.log("ViewCodeBasedBotComponent constructor")
}
ngOnInit(){
console.log("ViewCodeBasedBotComponent init")
}
ngOnDestroy(): void {
console.log("ViewCodeBasedBotComponent destroyed")
}
}
app.module.ts
const routes: Route[] = [
{
path: "core", component: CoreWrapperComponent, children:[
{path: 'viewbots', component: ViewBotsComponent, children:
[
{path: 'codebased', component: ViewCodeBasedBotComponent},
{path: 'intelligent', component: ViewPipelineBasedBotsComponent},
// {path: '', component: ViewCodeBasedBotComponent}
]
},
{path: 'botdetail', component: BotDetailWrapperComponent, children:
[
{path: 'codebased/:id', component: CodeBasedBotDetailComponent},
{path: 'intelligent/:id', component: PipelineBasedBotDetailComponent},
]
},
{path: 'analytics', component: WrapperComponent, children:
[
{path: 'overview', component: OverviewComponent},
{path: 'users', component: UsersComponent},
{path: 'sessions', component: SessionsComponent},
{path: 'messages', component: MessagesComponent},
{path: 'platforms', component: PlatformsComponent},
{path: 'events', component: EventsComponent},
{path: 'engagement', component: EngagementComponent},
{path: 'usage', component: UsageComponent}
]
},
{path: 'customner', component: ViewCustomnerComponent},
{path: 'customner/create', component: CreateCustomnerComponent},
{path: 'enterpriseprofile', component: EnterpriseprofileComponent},
{path: 'profile', component: ProfileComponent},
{path: 'reports', component: ReportsComponent},
{path: 'documentation', component: DocumentationComponent},
// {path: 'buildbot/codebased', component: BuildCodeBasedBotComponent},
// {path: 'buildbot/intelligent', component: BuildPipelineBasedBotComponent},
{path: 'buildbot', component: BuildbotWrapperComponent, children:
[
{path: 'codebased', component: BuildCodeBasedBotComponent},
{path: 'intelligent', component: BuildPipelineBasedBotComponent},
{path: '', component: ViewCodeBasedBotComponent}
]
},
// {path: 'viewbots/codebased', component: ViewCodeBasedBotComponent},
// {path: 'viewbots/intelligent', component: ViewPipelineBasedBotsComponent},
{path: 'login', component: LoginComponent, pathMatch: 'full'},
{path: 'login', component: LoginComponent, pathMatch: 'full'},
{path: '**', component: NotFoundComponent}
],
},
{path: "auth", component: AuthWrapperComponent, children: [
{path:'login', component: LoginComponent},
{path:'auth', component: SignupComponent}
]},
{path: '', redirectTo: 'core/viewbots/codebased',pathMatch: 'full'},
];
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
ViewBotsComponent,
ViewCodeBasedBotComponent,
ViewPipelineBasedBotsComponent,
BotPreviewCardComponent,
NotFoundComponent,
BuildCodeBasedBotComponent,
BuildPipelineBasedBotComponent,
RouterFragmentActiveDirective,
BasicInfoFormComponent,
AvatorFormComponent,
PipelineComponent,
KnowledgeBaseComponent,
CodeInputComponent,
IntegrationOptionListComponent,
CodeEditorComponent,
IntegrationItemComponent,
DraggableDirective,
DropTargetDirective,
DocumentationComponent,
ViewCustomnerComponent,
CreateCustomnerComponent,
OverviewComponent,
UsersComponent,
SessionsComponent,
MessagesComponent,
PlatformsComponent,
EventsComponent,
EngagementComponent,
UsageComponent,
ProfileComponent,
EnterpriseprofileComponent,
WrapperComponent,
ReportsComponent,
ChartComponent,
BuildbotWrapperComponent,
LoginComponent,
CoreWrapperComponent,
SignupComponent,
AuthWrapperComponent,
PipelineTestComponent,
BotDetailComponent,
HandsontableComponent,
SmartTableComponent,
BotDetailWrapperComponent,
CodeBasedBotDetailComponent,
PipelineBasedBotDetailComponent,
BotTestingComponent,
ConsumersComponent,
SessionComponent,
BotSessionsComponent,
ChatWrapperComponent,
ChatWindowComponent,
ChatMessageComponent,
BotWelcomeComponent,
ChatListComponent,
ChatItemComponent,
ChatroomComponent,
ScrollerDirective,
ReportDetailsComponent,
ReportDisplayComponent,
ReportControlsComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(routes), // RouterModule.forRoot(routes, { useHash: true }), if this is your app.module
BsDropdownModule.forRoot(),
TabsModule.forRoot(),
AceEditorModule,
UiSwitchModule,
FormsModule,
DragAndDropModule.forRoot(),
ChartModule,
NgxsModule.forRoot([AuthStateReducer,
NavigationStateReducer,
AppStateReducer,
EnterpriseprofileStateReducer,
ViewBotStateReducer,
ChatSessionStateReducer,
BotCreationStateReducer,
AnalysisStateReducer
]),
NgxsReduxDevtoolsPluginModule.forRoot(),
NgxsLoggerPluginModule.forRoot(),
HttpClientModule,
DragulaModule,
HotTableModule,
Ng2CompleterModule,
Ng2SmartTableModule,
BsDatepickerModule.forRoot(),
TooltipModule.forRoot()
],
providers: [DragService, AimService,{
provide: NGXS_PLUGINS,
useValue: persistPlugin,
multi: true
}],
bootstrap: [AppComponent]
})
export class AppModule {
}