亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重置 ReplaySubject 值

重置 ReplaySubject 值

慕森王 2023-05-19 14:56:21
我有這樣的文件結構:門面.ts狀態.ts組件.ts在facade我有:...getArticles(): Observable<Article> {  return this.articleService.fetchAll();}...在state我有:...triggerArticlesFetch$ = new ReplaySubject<void>(1);articles$ = this.triggerArticlesFetch$.pipe(  switchMap(() => this.fc.getArticles()),  shareReplay(1));loadArticles(): void {  this.triggerArticlesFetch$.next();}getArticles(): Observable<ArticleShare[]> {  return this.articles$;}...最后component我有:...`  <span> {{ articles$ | async | json }} </span>`......articles$: Observable<Article> = this.st.getArticles();ngOnInit() {  this.st.loadArticles();}...但是我在“使”此數據“無效”方面遇到了一些問題(組件是可重用的 - 并且狀態包含以前的數據)。[]/null articles$每次調用時清除/重置(可能在狀態文件中)的最佳方法是什么loadArticles()?
查看完整描述

1 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

從我的評論(據我了解)展開,每次初始化組件時,可觀察對象都應以[](or ) 開頭。null我能想到的最快解決方法是將 a 管道輸入startWith到源代碼中。


嘗試以下


狀態


...

triggerArticlesFetch$ = new ReplaySubject<void>(1);


articles$ = this.triggerArticlesFetch$.pipe(

  switchMap(() => 

    this.fc.getArticles().pipe(

      startWith([]) // or `null` as per requirement

    )

  ),

  shareReplay(1)

);


查看完整回答
反對 回復 2023-05-19
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號