在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[]>;
这是说属性'switchMap'在'Observable <[{} [],{} []]>'''类型上不存在。
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);
}));
}