【问题标题】:observable deprecated in Angular component class in (RXJS 6.X) [duplicate]在(RXJS 6.X)中的Angular组件类中不推荐使用可观察的[重复]
【发布时间】:2021-08-31 22:17:11
【问题描述】:

下面是我的组件方法

if (id !== undefined && id != null && id !== 0) {
              this.dataService.getTransactionById(id).subscribe(
                
                (tr: ITransactionResponse) => {
                if (tr != null) {
                   this.transaction = tr;
                }
                else {
                  this.router.navigate(['/']);
                }
              });

已弃用:但它在我的 Angular 项目中显示为已弃用。请让我知道我可以改变这种方法吗?

编辑:

我已将方法更改为以下代码 (RXJS 6)

 this.tranService.getTransactionById(id).subscribe({
            next: (result: any)  => {            
              this.transaction = result;
            },
            error: (error:any) => {
               this.router.navigate(['/transaction/transaction-list']);
            },
            complete: () => {
              //console.log('complete');
            }
          }); 

但仍显示已弃用

【问题讨论】:

    标签: angular typescript rxjs


    【解决方案1】:

    我认为你很好,所有可能的覆盖中只有一种已被弃用。 请参阅此问题进行更深入的讨论:Subscribe is deprecated: Use an observer instead of an error callback

    对于您最初的 If-Clause,我认为您可以将其更改为相同的结果:

    if(!id) { .... }
    

    【讨论】:

    • 如何根据我的要求进行更改?我很困惑?
    • 我认为您可以忽略弃用警告。这是 Visual Studio Code 的工具问题,不会以任何方式影响您的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2019-01-21
    相关资源
    最近更新 更多