我在门面上有这个
chatMessagesState$ = this.store.pipe(select(ChatMessageSelectors.getChatMessagesState));
this on selector
export const getChatMessagesState = createSelector(
getChatMessages,
(payload: ChatMessagesState) => payload
);
和这个组件
this.chatMessagesSub = this.chatMessagesFacade.chatMessagesState$.subscribe(state => {
if (state.messages) {
// do something
}
});
如何获取状态而不订阅?
constructor(private storeState: State<YourState>)
const state = this.storeState.value['your-state'];
如果你只需要取一次值
一种方法是将Observable
转换为Promise
并使用async-await
:
async getStateValue(){
const state=await this.chatMessagesFacade.chatMessagesState$.toPromise();
return state;
}
注意:这种方法违背了可观察对象的目的,因为您将无法侦听发出的更改。Promise将只解析/拒绝一次。