不能将void类型的函数分配给Observable <any>类型

我想搜索从API提取的用户列表。我首先初始化列表,然后希望能够过滤列表的用户名。我已经建立了异步可观察的逻辑。但是在我的第二个函数中。 searchList()this.userList => Function of type void can't be assigned to type Observable<any>和控制台中出现错误:

  

_co.searchChanged不是函数

我真的不知道为什么会出错。

我的代码:

service.ts

// get the user list from the api
getList(offset = 0): Observable<any> {
    return this.http.get(`${this.url}/users?offset=${offset}&limit=10`)
  }

page.ts

 public searchTerm: string = "";
 userList: Observable<any>;

constructor(private userService: UserService) {}

 ngOnInit() {

    this.getallUsers();   // intialize the userList which should be searched
 }

  // function to map the users
  getallUsers() { 
    this.userList = this.userService.getList(this.offset) 
    .pipe(map(response => response.results));
  }

  // filter the users for username
  filterUsers(searchTerm) {
    this.userList.subscribe(res => {
      const result = res.filter(user => {
        return user.username.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
      });
      return result;
    });
  }

  searchList() {
    this.userList = this.filterUsers(this.searchTerm);  // here I get the error
  }

page.html

<ion-searchbar mode="ios" class="items-searchbar" animated mode="ios" [(ngModel)]="searchTerm" (ionChange)="searchList()" placeholder="Filter by name..."></ion-searchbar>
...
<ion-list>
    <ion-item  lines="none" *ngFor="let user of (userList | async); let i = index">
      </ion-item>
  </ion-list>  
lihuiflora 回答:不能将void类型的函数分配给Observable <any>类型

  

不能将void类型的函数分配给Observable类型。

错误消息表明print(file2)期望分配一个可观察对象,但是.print不返回任何内容,因此返回dir(file2)并进行分配。

修改userList函数以返回可观察值。我正在使用map()运算符来转换结果。

filterUsers()
本文链接:https://www.f2er.com/3157644.html

大家都在问