我试图在我的ngrx存储中存储OrderState
对象。我有这个问题是从我的用户界面提取数据。我只想使用异步管道从此可观察对象获取一个子属性。如何使用管道从可观察对象中提取属性
order.state.ts
export interface OrderState {
stage: number;
postcode: string;
}
app.component.ts
...
export class AppComponent {
counter: Observable<number>;
readonly orderState$: Observable<OrderState>;
constructor(
private appStateStore: Store<AppState>,
private counterActions: CounterActions,
private orderStateStore: Store<OrderState>,
private orderActions: OrderActions,
) {
this.counter = appStateStore.select('counter');
this.orderState$ = orderStateStore.select(store => store);
}
...
app.component.html
<div>Stage: {{ (orderState$ | async)?.stage }}</div>
什t是从我的orderstate
对象获取stage
属性所需的语法。
如果我显示{{ orderState$ }}
我只是得到[Object object]
显示在页面上。
我猜可能我没有从我的商店中正确选择?
更新:我必须在这里丢失一些基本的东西。现在我已经做到了这一点,以尝试和工作发生了什么事......
app.component.ts
stage: number
...
this.orderStateStore.select(s => s).subscribe((data:OrderState) => {
this.stage = data.stage;
console.log(this.stage);
});
app.component.html
<div>Stage: {{ stage }}</div>
我可以在控制台中看到我的那个阶段报告为未定义。我相信这与问题有关。出于某种原因,我无法在此箭头功能中设置我的组件的stage
。
更新:此行为似乎很奇怪...
order.state.ts
export interface OrderState {
stage: number;
postcode: string;
}
app.component.ts
...
this.orderStateStore.select(s => s).subscribe((data:OrderState) => {
console.log('--- START ---');
console.log(data);
console.log('Stage: ' + data.stage);
console.log('--- END ---');
});
...
现在这是我得到的安慰乐...
正如你所看到的,data
实际上不是OrderState
类型。它是围绕OrderState
的包装类型,称为AppState
。很好,所以我修改我的代码以记录data.orderState.stage
以获得值1
。然而,当我这样做时,TypeScript抱怨orderState
不是数据的属性 - 它显然正在查看控制台输出!
'{{(orderState $ | async).stage}}'<==这个语法应该工作得很好。 – Meeker
可悲的是没有。不知道我做错了什么。 – Remotec
您应该用您的示例创建plnkr – Meeker