RxJS:如何在 angular 2 中的反应式搜索之前进行一些清理

RxJS: How to do some clean-up before reactive search in angular 2

这是我的场景

我想响应式搜索 如果值 length > 2 执行 api 搜索 否则清除数据。

但是我不知道清除数据

userList: Observable<User[]>; 

当长度 <= 2 时,userList 应该为空

另一个问题是为什么 Rxjs 运算符在 angular 2 中不起作用?

this.userList = this.userNameCtrl.valueChanges.do(d => console.log(d));

这是我的代码:

this.userList = this.userNameCtrl.valueChanges
            .filter(x => x.length > 2)
            .debounceTime(400)
            .distinctUntilChanged()
            .switchMap(value => this._userService.searchUsers(value));

I want to reactive search if the value length > 2 do api search else clear data.

如果我理解正确的话,清除你的意思是将空数组向下推到流中:

this.userList = this.userNameCtrl.valueChanges
    .debounceTime(400)
    .distinctUntilChanged()
    .switchMap(value => {
        if(value.length > 2) {
            return this._userService.searchUsers(value);
        } else {
            return Observable.of([]);
        }
    });

The other question is why Rxjs do operator not work in angular 2?

this.userList = this.userNameCtrl.valueChanges.do(d => console.log(d));

此代码不会输出任何内容,因为您需要先订阅 observable

this.userList = this.userNameCtrl.valueChanges
    .do(d => console.log(d))
    subscribe(() => {});

// or simply

this.userList = this.userNameCtrl.valueChanges
    subscribe(d => console.log(d));