我正在尝试订阅服务主题的组件,以确保组件订阅在服务发射上运行.
问题是订阅命中率仅在第一次订阅后也没有达到第二次附加的可观察返回0订阅.
以下是代码:
export class StoreService { private storesList : Subject<Array<KeyValue<string>>> = null; constructor(private http: Http,private _uService: UserService) { this.storesList = new Subject<Array<KeyValue<string>>>(); } loadStores(): void{ this.getAllStores().subscribe(m=>{ debugger; this.storesList.next(m); }); } storeListEvent(): Observable<Array<KeyValue<string>>>{ return this.storesList.asObservable(); } }
虽然组件是.
export class HeaderNavComponent implements OnInit,AfterViewInit,OnDestroy { private storeUpdateSubscription = null; constructor(private _userService: UserService,private cdRef: ChangeDetectorRef,private _storeService: StoreService,private router: Router,private location: Location) { this.storeUpdateSubscription = this._storeService.storeListEvent().subscribe(stores => { debugger; this.appStore = stores; this.verifySuperAdmin(); }); }
调用Store Service – loadStores时