订阅显示过时的Angular

huangapple go评论60阅读模式
英文:

Subscribe showing deprecated Angular

问题

我正在尝试编写一个电影应用程序,但我收到了这个消息:

"'(next?: ((value: any) => void) | null | undefined, error?: ((error: any) => void) | null | undefined, complete?: (() => void) | null | undefined): Subscription' 已弃用.ts(6385)
Observable.d.ts(51, 9): 在这里标记为已弃用。"

这是我的代码示例:

registerUser(): void {
    this.fetchApiData.userRegistration(this.userData).subscribe((result) => {
      // 此处放置成功用户注册的逻辑!(待实现)
      this.dialogRef.close(); // 成功后关闭模态框!
      this.snackBar.open(result, 'OK', {
        duration: 2000
      });
    }, (result) => {

希望这有所帮助!

英文:

I am trying to code a movie app and I am getting this message:

"'(next?: ((value: any) => void) | null | undefined, error?: ((error: any) => void) | null | undefined, complete?: (() => void) | null | undefined): Subscription' is deprecated.ts(6385)
Observable.d.ts(51, 9): The declaration was marked as deprecated here."

This is what my code looks like:

registerUser(): void {
    this.fetchApiData.userRegistration(this.userData).subscribe((result) => {
      // Logic for a successful user registration goes here! (To be implemented)
      this.dialogRef.close(); // This will close the modal on success!
      this.snackBar.open(result, 'OK', {
        duration: 2000
      });
    }, (result) => {

Can someone please help and thanks!

I am trying to make subscribe work.

答案1

得分: 1

警告信息与 subscribe() 方法的使用有关。在 RxJS 的更新版本中,subscribe() 方法的签名已经更改,旧的签名现在已被标记为不推荐使用。

更新后的代码:

registerUser(): void {
  this.fetchApiData.userRegistration(this.userData).subscribe({
    next: (result) => {
      // 在此处处理成功的用户注册逻辑!(待实现)
      this.dialogRef.close(); // 这将在成功时关闭模态框!
      this.snackBar.open(result, 'OK', {
        duration: 2000
      });
    },
    error: (error) => {
      // 处理错误情况
    },
    complete: () => {
      // 处理完成情况
    }
  });
}

subscribe() 方法现在接受一个包含 next、error 和 complete 属性的对象作为参数。您可以为每个属性提供相应的函数来处理可观察对象的下一个值、错误和完成事件。

希望这对您有所帮助。

英文:

Deprecation warning you are seeing is related to the usage of the subscribe() method. In newer versions of RxJS, the signature of the subscribe() method has changed, and the old signature is now marked as deprecated.

updated code:

registerUser(): void {
  this.fetchApiData.userRegistration(this.userData).subscribe({
    next: (result) => {
      // Logic for a successful user registration goes here! (To be implemented)
      this.dialogRef.close(); // This will close the modal on success!
      this.snackBar.open(result, 'OK', {
        duration: 2000
      });
    },
    error: (error) => {
      // Handle error cases
    },
    complete: () => {
      // Handle completion cases
    }
  });
}

subscribe() method now accepts an object as an argument with properties next, error, and complete. You can provide the corresponding functions for each property to handle the next value, errors, and completion of the observable.

hope it helpful.

答案2

得分: 0

新版本的 subscribe 方法接受一个包含 3 个可选字段的对象参数:

export interface Observer<T> {
  next: (value: T) => void;
  error: (err: any) => void;
  complete: () => void;
}

所以 .subscribe(result => ...) 应该替换为 .subscribe({next: (result) => ...})

英文:

The new version of subscribe method accepts object parameter with 3 optional fields:

export interface Observer&lt;T&gt; {
  next: (value: T) =&gt; void;
  error: (err: any) =&gt; void;
  complete: () =&gt; void;
}

So .subscribe(result =&gt; ...) should be replaced with .subscribe({next: (result) =&gt; ...})

huangapple
  • 本文由 发表于 2023年6月30日 05:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定