在NgRx中,按需加载模块时,要删除已注册的NgRx节点,可以使用StoreModule
的forFeature
方法来注册模块,并使用StoreModule
的unregister
方法来注销模块。
下面是一个示例代码,展示了如何按需加载模块并删除已注册的NgRx节点:
NgRx
模块,并使用forFeature
方法注册它。import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StoreModule } from '@ngrx/store';
import { featureReducer } from './feature.reducer';
@NgModule({
imports: [
CommonModule,
StoreModule.forFeature('feature', featureReducer)
]
})
export class FeatureModule { }
StoreModule
的unregister
方法来注销该模块。import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StoreModule } from '@ngrx/store';
import { featureReducer } from './feature.reducer';
@NgModule({
imports: [
CommonModule,
StoreModule.forFeature('feature', featureReducer)
]
})
export class FeatureModule {
constructor(private storeModule: StoreModule) {
// 注销已注册的NgRx节点
this.storeModule.unregister('feature');
}
}
请注意,使用unregister
方法只能在需要删除节点的模块中调用。