首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型Observable<Observable<any[]>>不能指定为Observable<any[]>

类型Observable<Observable<any[]>>不能指定为Observable<any[]>
EN

Stack Overflow用户
提问于 2018-03-06 12:32:54
回答 2查看 8.7K关注 0票数 7

我正在用来自数据库服务的数据实现自动完成:

代码语言:javascript
复制
@Injectable()
export class SchoolService {
  constructor(private db: AngularFirestore) {
  }

  getSchools(): Observable<School[]> {
    return this.db.collection<School>('schools').valueChanges();
  }
}

在我的部分:

代码语言:javascript
复制
export class SchoolComponent implements OnInit {
  formControl: FormControl = new FormControl();
  schools: Observable<School[]>;
  filteredSchools: Observable<School[]>;

  constructor(private schoolService: SchoolService) {
  }

  ngOnInit() {
    this.schools = this.schoolService.getSchools();

    //Below line gives error "Type Observable<Observable<School[]>> is not assignable to type Observable<School[]>".
    this.filteredSchools = this.formControl.valueChanges.pipe(
      startWith(''),
      map(name => this.filterSchools(name))
    );
  }

  filterSchools(name: string): Observable<School[]> {
    return this.schools.map(school => school.filter(s => s.name.toLowerCase().includes(name)));
  }
}

我的html:

代码语言:javascript
复制
<form>
  <mat-form-field>
    <input type="text" matInput placeholder="Type Your School to Continue" [matAutocomplete]="auto"
           [formControl]="formControl">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let school of filteredSchools | async" [value]="school">
        <span>{{school.name}}</span> |
        <small>{{school.city}}</small>
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

ngOnInit中的函数给出了Type Observable<Observable<School[]>> is not assignable to type Observable<School[]>错误。我怎么才能解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-06 12:58:48

这个错误准确地说明了出了什么问题。你的定义是:

代码语言:javascript
复制
filteredSchools: Observable<School[]>;

但是稍后您创建了一个可观测的filterSchools(name: string): Observable<School[]>结果(映射每个值),它也返回一个可观测的结果,所以在最后,您有一个可观察的Observable<Observable<School[]>>链。

实际上,您只想使用switchMap (或mergeMapconcatMap)而不是mapswitchMap将订阅嵌套的可观测值,并将链转换为Observable<School[]>

代码语言:javascript
复制
this.filteredSchools = this.formControl.valueChanges.pipe(
  startWith(''),
  switchMap(name => this.filterSchools(name))
);
票数 15
EN

Stack Overflow用户

发布于 2018-03-06 12:47:12

我建议使用Observable.combineLatest(.)

代码语言:javascript
复制
public ngOnInit(): void {
   this.schools = this.schoolService.getSchools();

   this.filteredSchools = Observable.combineLatest(
      this.formControl.valueChanges.startWith(''),
      this.schools
   ).map(([filter, schools]) => {
       if (!filter || filter === '') {
          return schools;
       }

       return schools.filter(s => s.name.toLowerCase().includes(name));
   });
}

仍然有一个寒冷的观察结束,只有当其中一个观测者发出新的数据时才会触发。

不是您的问题,而是模板自动完成,您将需要一个displayWith函数。

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

https://stackoverflow.com/questions/49130956

复制
相关文章

相似问题

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