类型 Observable<Observable<any[]>> 不可分配给类型 Observable<any[]>

Type Observable<Observable<any[]>> is not assignable to type Observable<any[]>

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

@Injectable()
export class SchoolService {
  constructor(private db: AngularFirestore) {
  }

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

在我的组件中:

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:

<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[]> 错误。我该如何解决这个问题?

我建议使用 Observable.combineLatest(...)

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));
   });
}

最后仍然有一个冷的 observable,只有当其中一个 observable 发出新数据时才会触发。

不是你的问题,但对于你的模板自动完成,你需要一个 displayWith 函数。

错误准确说明了错误所在。您定义了:

filteredSchools: Observable<School[]>;

但稍后你创建了一个 Observable,它发出(映射每个值)filterSchools(name: string): Observable<School[]> 的结果,其中 returns 也是一个 Observable,所以最后你有一个 Observable<Observable<School[]>> 的 Observable 链.

您实际上只是想使用 switchMap(或 mergeMapconcatMap)而不是 mapswitchMap 将订阅嵌套的 Observable 并将链变成 Observable<School[]>:

this.filteredSchools = this.formControl.valueChanges.pipe(
  startWith(''),
  switchMap(name => this.filterSchools(name))
);