我正在使用Subject
来展示组件:
private componentsState$: BehaviorSubject<{[key: string]: boolean}> =
new BehaviorSubject(this.getFormComponentState());
我有几个组成部分:
private swapComponent(componentName: string): void {
let componentsState = {
'list': (): void => this.componentsState$.next(this.getListComponentState()),
'form': (): void => this.componentsState$.next(this.getFormComponentState()),
'detail': (): void => this.componentsState$.next(this.getDetailComponentState()),
};
componentsState[componentName]();
}
因此,为了改变组件中的状态或发出一个值:
private getListComponentState(): {[key: string]: boolean} {
return { list: true, form: false, detail: false};
}
private getFormComponentState(): {[key: string]: boolean} {
return { list: false, form: true, detail: false};
}
private getDetailComponentState(): {[key: string]: boolean} {
return { list: false, form: false, detail: true};
}
在模板上:
<div *ngIf="(componentsState$ | async)?.list" payment-list></div>
<div *ngIf="(componentsState$ | async)?.form" payment-form></div>
<div *ngIf="(componentsState$ | async)?.detail" payment-detail></div>
但是,有时我会收到这样的错误:
错误类型错误:jit_unwrapValue12(...)未定义
堆栈跟踪:
[object Object] PaymentList.ngfactory.js:109:6
View_PaymentList_1 ng:///PaymentModule/PaymentList.ngfactory.js:109:6
ac_vendor<["./node_modules/@angular/core/@angular/core.es5.js"]/</DebugContext_.prototype.logError
ac_vendor<["./node_modules/@angular/core/@angular/core.es5.js"]/</ErrorHandler.prototype.handleError
ac_vendor<["./node_modules/@angular/core/@angular/core.es5.js"]/</PlatformRef_.prototype._bootstrapModuleFactoryWithZone/</<.next
ac_vendor<["./node_modules/@angular/core/@angular/core.es5.js"]/</EventEmitter.prototype.subscribe/schedulerFn<
ac_vendor<["./node_modules/rxjs/Subscriber.js"]/SafeSubscriber.prototype.__tryOrUnsub
ac_vendor<["./node_modules/rxjs/Subscriber.js"]/SafeSubscriber.prototype.next
ac_vendor<["./node_modules/rxjs/Subscriber.js"]/Subscriber.prototype._next
ac_vendor<["./node_modules/rxjs/Subscriber.js"]/Subscriber.prototype.next
ac_vendor<["./node_modules/rxjs/Subject.js"]/Subject.prototype.next
ac_vendor<["./node_modules/@angular/core/@angular/core.es5.js"]/</EventEmitter.prototype.emit
ac_vendor<["./node_modules/@angular/core/@angular/core.es5.js"]/</NgZone.prototype.triggerError
ac_vendor<["./node_modules/@angular/core/@angular/core.es5.js"]/</NgZone.prototype.forkInnerZoneWithAngularBehavior/this.inner<.onHandleError
ac_polyfills<["./node_modules/zone.js/dist/zone.js"]/</</ZoneDelegate.prototype.handleError
ac_polyfills<["./node_modules/zone.js/dist/zone.js"]/</</Zone.prototype.runTask
ZoneTask/this.invoke
有什么想法吗?