我的代码和distinctUntilChanged操作符有问题。
这是我的代码:
ngAfterViewInit() {
fromEvent(this.headlineInput.nativeElement, 'blur').pipe(
takeUntil(this.unsubscribe$),
map((evt: any) => evt.target.value),
distinctUntilChanged()
).subscribe((text: string) => {
this.onInputValueChanges(text);
});
}在这段代码中,我想在模糊上运行this.onInputValueChanges(text)方法,但只在标题输入发生变化时才运行。如果不是,则不应在subscribe中运行此方法。我认为我可以使用distinctUntilChanged,但它似乎不起作用。每当我的标题输入模糊时,它就会运行。我做错了什么吗?有人能给我指出这段代码的错误之处吗?谢谢!
发布于 2020-10-31 00:27:10
如果你想变得懒惰,你可以只检查对象的值是否与之前的发射不同。
ngAfterViewInit() {
fromEvent(this.headlineInput.nativeElement, 'blur').pipe(
takeUntil(this.unsubscribe$),
map((evt: any) => evt.target.value),
// don't run if the stringified version of the object is the same.
distinctUntilChanged((pre: any, curr: any) => JSON.stringify(pre) === JSON.stringify(curr)),
).subscribe((text: string) => {
this.onInputValueChanges(text);
});
}https://stackoverflow.com/questions/64611512
复制相似问题