我使用 Webstorm 编辑器并使用 Angular 15。 我收到以下有关订阅功能的消息:“已使用已弃用的符号” 我在 rxjs 网站上找到了这篇文章,该方法已更改: Angular 11:订阅已被弃用:使用观察者代替?
所以我更新了我的代码:
test() {
// Recommended
this.backendServices.getAllBox().subscribe(
(response) => {
console.log(response);
},
(error) => {
console.log(error);
},
() => {
console.log('complete');
}
);
}
Angular.json 和 package.json 已更新如下:
但是错误仍然存在。
缺少什么?
只是 subscribe 的调用签名已被弃用。
而不是:
obs$.subscribe(() => {}, () => {}, () => {})
您应该使用当前推荐的方式:
obs$.subscribe({next:() => {},error: () => {], complete: () => {}})
来自 Observable.ts:
subscribe(observer?: Partial<Observer<T>>): Subscription;
subscribe(next: (value: T) => void): Subscription;
/** @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 */
subscribe(next?: ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Subscription;