【问题标题】:Typescript Deprecation warning打字稿弃用警告
【发布时间】:2021-12-13 14:06:32
【问题描述】:

我正在使用此代码登录,但订阅显示已被贬低。我是打字稿新手,谁能帮帮我

doLogin() 
{
  
     this.userService.doLogin(this.loginForm.value).subscribe(
     result => 
     {
     console.log(result);
     localStorage.setItem('userData', JSON.stringify(result));
     this.router.navigate(['/list-user']);
     this.toastr.success('Success', 'Logged In Successfully');
     },
     (error) => 
     {
     console.log(error);
     this.toastr.error('Failed', 'Invalid Credentials');
     });
}

这适用于我参与过的另一个项目,但此处显示已被贬低 我收到了这个错误:

@deprecated — 不使用单独的回调参数,而是使用观察者参数。带有单独回调参数的签名将在 v8 中被删除。详情:https://rxjs.dev/deprecations/subscribe-arguments

'(next?: ((value: Object) => void) | null | undefined, error?: ((error: any) => void) | null | undefined, complete?: (() => void ) | null | undefined): Subscription' 已弃用.ts(6385) Observable.d.ts(55, 9):声明在此处被标记为已弃用。

【问题讨论】:

  • 请添加您看到的错误。
  • 请用此错误更新您的问题。
  • 格式化后请添加代码
  • 代码格式化是必要的

标签: angular typescript subscribe


【解决方案1】:

根据链接

import { of } from 'rxjs';
// recommended 
of([1,2,3]).subscribe((v) =>         
console.info(v));
// also recommended
of([1,2,3]).subscribe({
    next: (v) => console.log(v),
    error: (e) => console.error(e),
    complete: () =>     
        console.info('complete') 
})

您将不再需要传递多个回调。 如果需要,您可以传入一个包含回调的可观察对象。

【讨论】:

    猜你喜欢
    • 2016-07-21
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2021-07-12
    相关资源
    最近更新 更多