此处 ItemService
项目.服务.ts
export const items = (state: any = [], {type, payload}) => {
switch (type) {
default:
return state;
}
};
export interface Item {
id: number;
name: string;
description: string;
};
export interface AppStore {
items: Item[];
selectedItem: Item;
};
@Injectable()
export class ListService {
items: Observable<Array<Item>>;
private listModel: ItemList = new ItemList();
constructor(private itemListApi: ItemListApi, private store: Store<AppStore>) {
this.items = store.select('items');===>Error
}
}
我在 this.items
中遇到错误。错误是“Type 'Observable<{}>' is not assignable to type 'Observable”。什么地方出了错?有什么帮助吗?
最佳答案
发生这种情况是因为您定义了 items
作为Observable<Array<Item>>
同时store.select('items')
可能返回 Observable<T>
.
您可以使用类型转换告诉编译器您期望它返回什么:
this.items = <Observable<Item[]>>store.select('items');
关于angular - 类型 'Observable<{}>' 不可分配给类型 'Observable<Item[]> : Rxjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40628875/