Angular 11 - subscribe is deprecated: Use an observer instead of a complete callback

syn*_*ryd 5 rxjs angular

I have an Angular application in which errors are beginning to appear in the subscribe that they are deprecated.

This is my subscribe:

this.route.params.subscribe(params => {
      this.clienteId = +params['clienteId'];
      this.isNew = !this.clienteId;
      if (!this.isNew) {
        this.clienteService.get(this.clienteId).subscribe(c => {
          this.cliente = c;
          this.formNuevo.patchValue(this.cliente);
        });
      }
    });
Run Code Online (Sandbox Code Playgroud)

This is the client-service.ts (get):

public get(idClient: number): Observable<Client> {
      return this.http.get<Client>(`${PREFIX}/${idClient}`);
    }
Run Code Online (Sandbox Code Playgroud)

This is the error of the new subscribe:

在此输入图像描述

-> 'clienteId' : object access via string literals is disallowed (no-string-literal)tslint(1)

-> subscribe: subscribe is deprecated: Use an observer instead of a complete callback (deprecation)tslint(1)

How can I solve the problem and apply the changes to this subscribe?

Mic*_*l D 14

  1. 使用高阶映射运算符将switchMap一个可观察量映射到另一个可观察量。尽量避免嵌套订阅。

  2. null“错误”主要是由于在指定其他回调时尝试发送一个或另一个订阅回调而引起的。例如。使用nextcompletenullfor error。可以通过发送显式指定回调的观察者对象来解决。

尝试以下操作

import { iif, NEVER } from 'rxjs';
import { switchMap } from 'rxjs/operators';

this.route.params.pipe(
  switchMap(params => {
    this.clienteId = +params['clienteId'];
    this.isNew = !this.clienteId;
    return iif(                        // <-- `iif` for conditional mapping
      () => !this.isNew,
      this.clienteService.get(this.clienteId),
      NEVER                            // <-- don't emit if condition fails
    );
  })
).subscribe({
  next: c => {
    this.cliente = c;
    this.formNuevo.patchValue(this.cliente);
  },
  error: error => {
    // handle error
  },
  complete: () => {
    console.log('Request complete');
  }
});
Run Code Online (Sandbox Code Playgroud)