【问题标题】:Type Observable<Observable<any[]>> is not assignable to type Observable<any[]>类型 Observable<Observable<any[]>> 不可分配给类型 Observable<any[]>
【发布时间】:2018-08-14 07:38:03
【问题描述】:

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

@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&lt;Observable&lt;School[]&gt;&gt; is not assignable to type Observable&lt;School[]&gt; 错误。我该如何解决这个问题?

【问题讨论】:

标签: angular typescript rxjs rxjs5


【解决方案1】:

错误准确地说明了问题所在。你定义了:

filteredSchools: Observable<School[]>;

但稍后您创建了一个 Observable,它发出(映射每个值)filterSchools(name: string): Observable&lt;School[]&gt; 的结果,它也返回一个 Observable,因此最后您有一个 Observable&lt;Observable&lt;School[]&gt;&gt; 的 Observable 链。

实际上,您似乎只想使用switchMap(或mergeMapconcatMap)而不是mapswitchMap 将订阅嵌套的 Observable 并将链变成Observable&lt;School[]&gt;

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

【讨论】:

  • 谢谢。你启发了我。就我而言,switchMap(不是mergeMap)与自动完成中的预输入功能一起使用。您能否在我接受之前更新您的答案?
  • 好的,请注意 switchMapmergeMap 并不完全相同,即使它可以解决您的问题,但在您的用例中可能并不重要。
【解决方案2】:

我建议使用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));
   });
}

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

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

【讨论】:

  • 感谢您的回答并提醒我使用displayWith功能。 :)
猜你喜欢
  • 2017-08-12
  • 2023-04-10
  • 2018-08-30
  • 2020-05-28
  • 2018-03-09
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
相关资源
最近更新 更多