简体   繁体   English

TypeScript / RxJS-可观察的subscription()方法complete()未运行

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

I've had a good look around to try and solve this but can't find an answer that works. 我环顾四周以尝试解决此问题,但找不到有效的答案。

I'm trying to implement a callback for an additional function when a subscribe() method successfully returns an 'contacts$' observable, but using complete() on the subscription does not do anything. 我正在尝试在subscribe()方法成功返回可观察到的“ contacts $”时对其他函数实现回调,但在订阅上使用complete()却无济于事。

I've also tried using finally() on the observable as suggested elsewhere, but this also doesn't work. 我也尝试过在其他地方建议在可观察对象上使用finally(),但这也不起作用。

Using complete(): 使用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();
        }
    )
}

Using finally(): 使用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();
            }
    )
}

Method for callback on observable completion: 可观察完成时回调的方法:

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

Difficult to say without more info, but I'll give a shot: 在没有更多信息的情况下很难说,但我会给您一个镜头:

  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));
        }
      )
  }

And : 和:

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

Or a little cleaner : 或稍微清洁一点:

    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));
    }

Lastly as @Picci suggested : 最后,如@Picci建议的那样:

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

Beware that all the above code is absoultely not tested and only for reference. 请注意,以上所有代码均未经测试,仅供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM