gpt4 book ai didi

angular - 无法在 combineLatest 上使用管道

转载 作者:行者123 更新时间:2023-12-03 18:40:43 30 4
gpt4 key购买 nike

我正在尝试关注 this example 但在使用 combineLatest() 时遇到问题:

Property 'subscribe' does not exist on type 'OperatorFunction<unknown, [unknown, Item[], User[]]>'



这是我的门面:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { combineLatest, distinctUntilChanged, map } from 'rxjs/operators';

export interface User {
test: string;
}

export interface Item {
test: string;
}

interface State {
items: Item[];
users: User[];
}

let _state: State = {
items: [],
users: [],
};

@Injectable()
export class Facade {
private store = new BehaviorSubject<State>(_state);
private state$ = this.store.asObservable();

items$ = this.state$.pipe(map(state => state.items), distinctUntilChanged());
emails$ = this.state$.pipe(map(state => state.users), distinctUntilChanged());

constructor(private http: HttpClient) {
combineLatest(this.items$, this.emails$).subscribe(
([items, users]) => {}
);
}
}
items$emails$ 都是 Observable。此代码几乎反射(reflect)了上面示例中的代码,为什么会出现此错误?

最佳答案

假设您使用的是 RxJS 6+ 版本, as per the migration docs :

来自 combineLatest

  • rxjs/operators 已被弃用。您现在应该直接导入该方法:
    import { combineLatest } from 'rxjs';
  • 新版本的 combineLatest() 接受一个 数组 Observables 作为它的参数:
    combineLatest([this.items$, this.emails$]).subscribe(([items, emails]) => {});
  • 关于angular - 无法在 combineLatest 上使用管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57172109/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com