首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >键入'Observable<Observable<Employee[]>>‘不能指定键入'Observable<string[]>’

键入'Observable<Observable<Employee[]>>‘不能指定键入'Observable<string[]>’
EN

Stack Overflow用户
提问于 2022-03-18 12:48:04
回答 2查看 112关注 0票数 -1

我试着用这种方法过滤一个自动完成的垫子:

ngOnInit 函数:

代码语言:javascript
复制
filterEmployee() {
    this.filteredOptions = this.employee.valueChanges.pipe(
      startWith(""),
      map((value) => this._filter(value))
    );
  }

滤波函数

代码语言:javascript
复制
private _filter(value: string): Observable<Employee[]> {
    const filterValue = value && value.toLowerCase();
    return of(
      this.employees.filter(
        (employee) =>
          employee.name.toLowerCase().indexOf(filterValue) > 1
      )
    );
  }

有个错误:

我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2022-03-18 12:51:24

您尝试返回可观察到的字符串数组,但您已经编写了可观察到的员工。您可以像这样更新Observable:

代码语言:javascript
复制
private _filter(value: string): Observable<string[]> {
    const filterValue = value && value.toLowerCase();
    return of(
      this.employees.filter(
        (employee) =>
          employee.name.toLowerCase().indexOf(filterValue) > 1
      )
    );
  }
票数 0
EN

Stack Overflow用户

发布于 2022-03-18 12:52:44

首先,不要map()到一个Observable (我的意思是,你可以,但这不是你想要的)。我想你把mapflatMap搞混了。

代码语言:javascript
复制
private _filter(value: string): Employee[] {
  const filterValue = value && value.toLowerCase();
    return employees.filter(
      (employee) =>
        employee.name.toLowerCase().indexOf(filterValue) > 1
    );
}

现在您将分配Observable<Employee[]>而不是Observable<Observable<Employee[]>>

我们能看到this.employeesEmployee的定义吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71527256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档