由于订阅 (ANGULAR),无法使用 res.name

Impossible to use res.name because of subscribe (ANGULAR)

目前我正在关注 Youtube 上的一个教程,其中它使用 Angular 进行身份验证,我现在无法继续:

他的代码在我这边不起作用,因为 subscribe() 给我消息:

@deprecated — Instead of passing separate callback arguments, use an observer argument. Signatures taking separate callback arguments will be removed in v8. Details: https://rxjs.dev/deprecations/subscribe-arguments

我被迫使用 nexterrorcomplete,尽管如此,res.name 仍未显示。

ngOnInit(): void {
  this.http.get(
    'http://localhost:8080/api/user',
    {withCredentials: true}
    ).subscribe({
    complete: () => { (res: any) => this.visiteur = `${res.nom}`  },
    error: () => { (err: any) => console.log(err)  },
    next: () => { (res: any) => this.visiteur = `${res.nom}`  }
    });
}

目前的订阅方式是使用更多 rxjs operators, The tutorial you are following seems a little outdated. I recommend you follow angular's Tour of heroes 教程。它总是 up-to-date.

这应该让你继续:

this.http
    .get('http://localhost:8080/api/user')
    .pipe(
        catchError(err => {
            console.log(err);
        })
    )
    .subscribe(data => {
        this.visiteur = `${data.nom}`;
    });

此外,我建议您实施 angular http interceptor 以传递所需的 http attributes/headers(在您的情况下为 withCredentials)。但这对于这个问题的范围是可选的。

最后,不要忘记避免订阅的 memory-leak 陷阱。