gpt4 book ai didi

javascript - Ngrx 选择器不触发更新

转载 作者:行者123 更新时间:2023-12-03 00:45:00 24 4
gpt4 key购买 nike

我的状态是:

export interface RequestState {
tabsContent: TabContent[];
}

其中 TabContent 表示可视选项卡数组,每个选项卡包含 1 个请求和附加信息:

export interface TabContent {
tabInfo: TabInfo;
request: Request;
results?: any[];
}

我有一个专门的选择器来从单个 TabContent 获取一个请求:

export const getRequestForTabInfo = (tabInfo: TabInfo) => createSelector(
getTabContent,
(tabContent: TabContent[]) => {
const tabContentFiltered = tabContent.filter(tab =>
tab.tabInfo.id === tabInfo.id
&& tab.tabInfo.index === tabInfo.index
&& tab.tabInfo.state === tabInfo.state);
if (tabContentFiltered && tabContentFiltered.length === 1) {
return tabContentFiltered[0].request;
}
return {} as Request;
},
);

这是获取所有 TabsContent 的基本方法:

export const getTabContent = createSelector(
getRequestFeatureState,
state => state.tabsContent,
);

基于此FeatureSelector:

const getRequestFeatureState = createFeatureSelector<RequestState>('requests');

在外观服务中定义:

tabsContent$: Observable<TabContent[]>;
constructor(private store: Store<fromRequest.State>) {
this.tabsContent$ = this.store.pipe(
select(fromRequest.getTabContent),
takeUntil(this.componentDestroy()),
);
}
public getRequest(tabInfo: TabInfo): Observable<Request> {
return this.store.pipe(
select(fromRequest.getRequestForTabInfo(tabInfo)),
takeUntil(this.componentDestroy()),
);
}

并在组件中使用:

private initializeComponent(): void {
this.requestFacade.getRequest(this.tabInfo).pipe(
takeUntil(this.componentDestroy()),
).subscribe(request => {
console.log('request : ', request);
this.request = request;
});
this.requestFacade.tabsContent$.pipe(
takeUntil(this.componentDestroy()),
).subscribe(tab => {
console.log('tab : ', tab);
});
}

构建组件时,我会收到两个日志,但是当我更新部分相关状态(更新一个请求的内容)时,仅出现 tab 日志。

为什么经过过滤的选择器 observable 没有执行任何操作?

最佳答案

您在 getRequestForTabInfo 选择器中使用静态参数,因此它假定它不会随时间变化,这就是您没有收到更改的原因。

我建议分派(dispatch)一个操作来设置存储中选定的 tabinfo 并使用选择器来检索它,而不是传递参数。您已经存储了 TabContents,因此应该能够重用您已有的一些逻辑。

关于javascript - Ngrx 选择器不触发更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282535/

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