Angular 11:订阅已弃用:改用观察者?

Smo*_*sky 14 typescript tslint angular

我的 tslint 疯了吗?它为我在整个应用程序中所做的每个订阅发出警告。无论我使用旧的还是新的语法都没有关系,它仍然说 subscribe 已被弃用......如何编写一个不会被弃用的订阅?

直到今天还好:

something.subscribe((user: User) => {
        this.userProviderService.setUserId(user.userId);
        this.proceed = true;
      });
Run Code Online (Sandbox Code Playgroud)

我尝试了新语法但没有改变:

something.subscribe({
        next: (user: User) =>  {
          this.userProviderService.setUserId(user.userId);
          this.proceed = true;
        },
        complete: () => {},
        error: () => {}
      });
Run Code Online (Sandbox Code Playgroud)

这正是它所说的:

(方法) Observable.subscribe(next?: (value: Object) => void, error?: (error: any) => void, complete?: () => void): Subscription (+4重载) @deprecated —使用观察者而不是完整的回调

@deprecated — 使用观察者而不是错误回调

@deprecated — 使用观察者而不是完整的回调

订阅已弃用:使用观察者而不是完整的回调(弃用)tslint(1)

那么我现在如何订阅东西?

Smo*_*sky 13

我刚刚在 VS Code 扩展选项卡中查找了 TSLint (v1.3.3)。它说:

重要提示:TSLint 已被弃用,取而代之的是 ESLint。

当我禁用 TSLint 并安装 ESLint 时,所有与订阅相关的警告都消失了。

干杯!


小智 12

要回答您的问题“那么我现在如何订阅事物”:https : //rxjs-dev.firebaseapp.com/guide/observer 就是这样。它易于使用并且与之前所做的非常相似,只是做了一些小改动,现在它实际上接受了一个带有 3 个键的对象(观察者):next、error、complete。

我们在工作中进行了与 2 天前相同的讨论,尽管您可以/应该使用观察者,但弃用似乎是错误的警报。(我们认为我们必须更改约 900 个订阅):

这是在 rxjs github 页面上创建的关于此问题的问题:https : //github.com/ReactiveX/rxjs/issues/6060

在其中开发人员说这是由于打字稿错误:https : //github.com/microsoft/TypeScript/issues/43053

这个错误已经在 3 天前修复了,但我不确定它是否已经在最新版本中:

https://github.com/microsoft/TypeScript/pull/43165


Was*_*siF 7

最新版本支持以下结构:

myObservable.subscribe({
    next: (val) => { ... },
    error: (err) => { ... },
    complete: () => { ... }     
})
Run Code Online (Sandbox Code Playgroud)