2

看起来 Rxjs 版本发生了一些变化,并且在尝试重做一些旧代码时遇到了真正的问题。

从我在这里阅读的其他帖子来看,我似乎需要使用管道,但是当我尝试修改代码时,我会遇到类型问题。

使用“rxjs”:“6.5.2”,“rxjs-compat”:“6.5.2”,

任何建议或帮助将不胜感激。

当前代码

import { Observable, combineLatest, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';

currentUserRoles: Array<string>;
currentUserRolesData: Array<Role> = [];
allRoles: Array<Role>;
combineRoles$: Observable<Role[]>;

它是说“Observable<[{}[], {}[]]>”类型上不存在属性“switchMap”。

async setCurrentUser(user: User): Promise<any> {
    this.currentUserRoles = user.roles;

    const rolesWithCustomerId = this.afs.collection('roles', ref =>
        ref.where('customerId', '==', user.customerId)
    );
    const rolesWithUserId = this.afs.collection('roles', ref =>
        ref.where('customerId', '==', user.id)
    );
    this.combineRoles$ = combineLatest(
        rolesWithCustomerId.valueChanges(),
        rolesWithUserId.valueChanges()
    ).switchMap((cities: [Role[], Role[]]) => {
        // tslint:disable-next-line:no-shadowed-variable
        const [rolesWithCustomerId, rolesWithUserId] = cities;
        const combined = rolesWithCustomerId.concat(rolesWithUserId);
        return Observable.of(combined);
    });
}

修订后的代码 错误类型“可观察”不可分配给“可观察”类型

async setCurrentUser(user: User): Promise<any> {
  this.currentUserRoles = user.roles;

  const rolesWithCustomerId = this.afs.collection('roles', ref =>
    ref.where('customerId', '==', user.customerId)
  );
  const rolesWithUserId = this.afs.collection('roles', ref =>
    ref.where('customerId', '==', user.id)
  );
  this.combineRoles$ = combineLatest(
    rolesWithCustomerId.valueChanges(),
    rolesWithUserId.valueChanges()
  )
  .pipe(switchMap((cities: [Role[], Role[]]) => {
    // tslint:disable-next-line:no-shadowed-variable
    const [rolesWithCustomerId, rolesWithUserId] = cities;
    const combined = rolesWithCustomerId.concat(rolesWithUserId);
    return Observable.of(combined);
  }));
}
4

0 回答 0