gpt4 book ai didi

javascript - EventEmitter 的上下文 'this' 未分配给 Observable 类型的 'this' 方法

转载 作者:行者123 更新时间:2023-11-30 20:03:36 25 4
gpt4 key购买 nike

我查看了 GitHub 存储库中的示例。应用程序成功执行并且编写的代码通常可以正常工作,但我的 Visual Studio Code 中出现以下错误:

The 'this' context of type 'EventEmitter' is not assignable to method's 'this' of type 'Observable'.\n Types of property 'lift' are incompatible.\n Type '(operator: Operator) => Observable' is not assignable to type '(operator: Operator) => Observable'.\n Type 'Observable' is not assignable to type 'Observable'.\n Type 'string' is not assignable to type 'R'.

产生错误的代码如下:

this.notificationService.notifier
.do(message => {
this.message = message;
this.snackVisibility = 'visible';
})
.switchMap(message => Observable.timer(2000))
.subscribe(timer => this.snackVisibility = 'hidden');

我所有的服务文件代码如下:

import { EventEmitter } from "@angular/core";

export class NotificationService {
notifier = new EventEmitter<string>();

notify(message: string) {
this.notifier.emit(message);
}
}

尽管该应用程序运行完美,但 VS Code 给出了如图所示的错误:

enter image description here

此错误的最佳解决方案是什么?

最佳答案

服务:

import { Subject } from "rxjs";

export class NotificationService {
private notifier = new Subject<string>();
public notifier$ = this.notifier.asObservable();

notify(message: string) {
this.notifier.next(message);
}
}

组件:

this.notificationService.notifier$
.subscribe(message => {
this.message = message;
this.snackVisibility = 'visible';
setTimeout(()=>{
this.snackVisibility = 'hidden';
}, 2000);
});

关于javascript - EventEmitter <string> 的上下文 'this' 未分配给 Observable <string> 类型的 'this' 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com