Typescript 如何实现ngxs动作的去抖动模拟?
我有搜索输入:Typescript 如何实现ngxs动作的去抖动模拟?,typescript,debouncing,ngxs,Typescript,Debouncing,Ngxs,我有搜索输入: <input type="text" (ngModelChange)="projectFilter()"> 一种方法是使用BehaviorSubject创建一个可观察链,该对象使用debounceTime()分派操作 //组件类中的某个地方 projectFilter$=新行为主体(“”) projectFilter(){ //设置下一个值而不是分派 this.projectFilter$.next('当前值') } 恩戈尼尼特(){ //每当使用debounce提
<input type="text" (ngModelChange)="projectFilter()">
一种方法是使用
BehaviorSubject
创建一个可观察链,该对象使用debounceTime()
分派操作
//组件类中的某个地方
projectFilter$=新行为主体(“”)
projectFilter(){
//设置下一个值而不是分派
this.projectFilter$.next('当前值')
}
恩戈尼尼特(){
//每当使用debounce提供新值时,进行debounce
此.projectFilter$.pipe(
去BounceTime(1000),
点击(()=>{
this.store.dispatch(新建MyAction())
})
).subscribe()
}
注意:代码未测试。用它来理解概念并相应地实施
或者,您可能希望使用被动形式。有了它,你就可以免费看到valueChanges
这样的东西
希望这有帮助
projectFilter() {
this.store.dispatch([
new SomeAction()
);
});
}
// somewhere in your component class
projectFilter$ = new BehaviorSubject<string>('')
projectFilter() {
// set next value instead of dispatching
this.projectFilter$.next('current value')
}
ngOnInit() {
// debounce whenever a new value is available with debounce
this.projectFilter$.pipe(
debounceTime(1000),
tap(() => {
this.store.dispatch(new MyAction())
})
).subscribe()
}