簡體   English   中英

TypeScript / RxJS-可觀察的subscription()方法complete()未運行

[英]TypeScript/RxJS - Observable subscribe() method complete() not running

我環顧四周以嘗試解決此問題,但找不到有效的答案。

我正在嘗試在subscribe()方法成功返回可觀察到的“ contacts $”時對其他函數實現回調,但在訂閱上使用complete()卻無濟於事。

我也嘗試過在其他地方建議在可觀察對象上使用finally(),但這也不起作用。

使用complete():

ngOnInit() {
    this.getContacts().subscribe(
        data => {
            this.contacts = data;
            console.log('NewInvoice.contacts:', data);
            this.selectedContactId = this.contacts[0].id;
            console.log('selectedContactId: ' + this.selectedContactId);
        },
        error => {
            console.error('Error getting contacts via subscribe() method:', error);
        },
        () => {
            this.getSelectedContact();
        }
    )
}

使用finally():

ngOnInit() {
    this.getContacts()
        .finally(() => console.log('a'))
        .subscribe(
            data => {
                this.contacts = data;
                console.log('NewInvoice.contacts:', data);
                this.selectedContactId = this.contacts[0].id;
                console.log('selectedContactId: ' + this.selectedContactId);
            },
            error => {
                console.error('Error getting contacts via subscribe() method:', error);
            },
            () => {
                this.getSelectedContact();
            }
    )
}

可觀察完成時回調的方法:

getSelectedContact() {
    this.contactsCollection.doc(this.selectedContactId).ref.get().then(snapshot => {
        this.selectedContact = snapshot.data() as Contact;
        console.log('selectedContact:', this.selectedContact);
    })
}

在沒有更多信息的情況下很難說,但我會給您一個鏡頭:

  ngOnInit() {
    this.getContacts()
      .subscribe(
        data => {
          this.contacts = data;
          console.log('NewInvoice.contacts:', data);
          this.selectedContactId = this.contacts[0].id;
          console.log('selectedContactId: ' + this.selectedContactId);
        },
        error => {
          console.error('Error getting contacts via subscribe() method:', error);
        },
        () => {
          this.getSelectedContact()
            .asObservable()
            .subscribe((a) => console.log(a));
        }
      )
  }

和:

getSelectedContact() {
    return this.contactsCollection.doc(this.selectedContactId).ref.get().then(snapshot => {
        this.selectedContact = snapshot.data() as Contact;
        console.log('selectedContact:', this.selectedContact);
    })
}

或稍微清潔一點:

    const callback = (a) => console.log(a);
  ...
    () => {
      this.getSelectedContact(callback);
    }
  ...
    getSelectedContact(callback) {
      this.contactsCollection.doc(this.selectedContactId).ref.get()
        .then(snapshot => {
          this.selectedContact = snapshot.data() as Contact;
          console.log('selectedContact:', this.selectedContact);
        })
        .then(a => callback(a));
    }

最后,如@Picci建議的那樣:

this.getContacts()
    .last()
    .exhaustMap((data) => this.getSelectedContact(data))
    .map(a => console.log(a))
    .subscribe();

請注意,以上所有代碼均未經測試,僅供參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM