订阅在 Ionic 中已被弃用

问题描述

我在 Ionic Proyect 中有此警告“订阅已弃用:使用观察者而不是完整回调”。请帮忙。

fetch(cb) {
    this.loadingIndicator = true;
    this.cservice.postNcRangoConta(this.body).subscribe(
      res => {
        try {
          if (res) {
            this.headers = Object.keys(res[0]);
            this.columns = this.getColumns(this.headers);
            this.temp = [...res];
            cb(res);
            this.loadingIndicator = false;
          }
        } catch (error) {
          this.loadingIndicator = false;
          this.rows = null;
          this.toast.presentToast('No se encontraron datos','warning');
        }
      },err => {
        console.log(err);
        if (this.desde || this.hasta) {

          this.loadingIndicator = false;
          this.toast.presentToast('La API no responde','danger');
        } else {
          this.loadingIndicator = false;
          this.toast.presentToast('Debe llenar las fechas','warning');
        }
      }
    );
  }

解决方法

subscribe 方法实际上并未被弃用,但您使用它的方式已被弃用。尝试切换到它的新语法。

// Deprecated
source.subscribe(
  (res) => cb(res),error => console.error(error),() => console.log('Complete')
);

// Recommended
source.subscribe({
  next: (res) => cb(res),error: error => console.error(error),complete: () => console.log('Complete')
});