Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Ngrx选择器不发出新值_Angular_Ngrx - Fatal编程技术网

Angular Ngrx选择器不发出新值

Angular Ngrx选择器不发出新值,angular,ngrx,Angular,Ngrx,我试图在解析程序中重新加载页面时从服务器获取单个对象,但是在保存到存储区后,应该从存储区获取该对象的选择器不会发出新值。为什么呢? 选择器中的日志被调用两次,第二次调用新值,但由于某些原因,它们不会在解析器中发出。 任何帮助都将不胜感激 我的减速器文件: export interface GroupState extends EntityState<Group> { topicsLoaded: { topic: string, coun

我试图在解析程序中重新加载页面时从服务器获取单个对象,但是在保存到存储区后,应该从存储区获取该对象的选择器不会发出新值。为什么呢? 选择器中的日志被调用两次,第二次调用新值,但由于某些原因,它们不会在解析器中发出。 任何帮助都将不胜感激

我的减速器文件:

    export interface GroupState extends EntityState<Group> {
    topicsLoaded: {
        topic: string,
        count: number,
        groupsLoaded: number
    }[];
}

export const adapter = createEntityAdapter<Group>();

export const initialGroupState = adapter.getInitialState({
    topicsLoaded: []
});

export const groupReducer = createReducer(
    initialGroupState,
    on(GroupActions.getTopicGroupsSuccess, (state, action) => {
        let oldTopics;
        let newTopics;
        // Check if 1st batch of groups have already been loaded //
        const oldGroupsLoaded = state.topicsLoaded.find(g => g.topic === action.topic);

        if (!oldGroupsLoaded) {
            // Add topic to group entity //
            newTopics = [...state.topicsLoaded];
            newTopics.push({
                topic: action.topic,
                count: action.count,
                groupsLoaded: action.groupsLoaded
            });
        } else {
            // Update group entity //
            const index = [...state.topicsLoaded].indexOf(oldGroupsLoaded);
            oldTopics = [...state.topicsLoaded];
            oldTopics[index] = {
                ...state.topicsLoaded[index],
                groupsLoaded: state.topicsLoaded[index].groupsLoaded + action.groupsLoaded
            };
        }
        return adapter.addMany(action.groups,
            {
                ...state,
                topicsLoaded: oldGroupsLoaded ? oldTopics : newTopics
            });
    }),
    on(GroupActions.getSingleGroupSuccess, (state, action) => {
        const newTopicLoaded = [...state.topicsLoaded];
        newTopicLoaded.push({
            topic: action.group.topic,
            count: action.count,
            groupsLoaded: 1
        });
        return adapter.addOne(action.group, {
            ...state,
            topicsLoaded: newTopicLoaded
        });
    })
);

export const {
    selectAll,
} = adapter.getSelectors();
导出接口GroupState扩展EntityState{
主题:{
主题:字符串,
计数:数字,
groupsLoaded:编号
}[];
}
export const adapter=createEntityAdapter();
export const initialGroupState=adapter.getInitialState({
topicsLoaded:[]
});
export const groupReducer=createReducer(
初始组状态,
on(GroupActions.GetTopicGroupsAccess,(状态,操作)=>{
让老话题;
让新托皮克;
//检查是否已加载第一批组//
constOldGroupsLoaded=state.topicsloadded.find(g=>g.topic==action.topic);
如果(!oldGroupsLoaded){
//将主题添加到组实体//
newTopics=[…state.topicsloadded];
推({
主题:action.topic,
count:action.count,
groupsLoaded:action.groupsLoaded
});
}否则{
//更新组实体//
const index=[…state.topicsloadded].indexOf(oldGroupsLoaded);
oldTopics=[…state.topicsLoaded];
旧主题[索引]={
…state.topicsLoaded[索引],
groupsLoaded:state.topicsLoaded[index]。groupsLoaded+action.groupsLoaded
};
}
返回adapter.addMany(action.groups,
{
……国家,
加载的主题:旧组已加载?旧主题:新主题
});
}),
on(GroupActions.getSingleGroupSuccess,(状态,操作)=>{
const newTopicLoaded=[…state.topicsLoaded];
纽托皮克洛德推({
主题:action.group.topic,
count:action.count,
组别:1
});
返回adapter.addOne(action.group{
……国家,
已加载主题:新主题已加载
});
})
);
出口常数{
选择全部,
}=adapter.getSelectors();
解析程序:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
        const topic = route.queryParams['topic'];
        const id = route.paramMap.get('id');
        return this.store
            .pipe(
                select(isTopicLoaded, { topic }),
                withLatestFrom(
                    this.store.pipe(select(selectTopicGroups, { topic })),
                    this.store.pipe(select(selectGroupById, { id }))
                ),
                tap(([loaded, groups, group]) => {
                    if (!loaded && !this.isLoading) {
                        this.isLoading = true;
                        this.store.dispatch(GroupActions.getTopicGroups({ topic, pageNumber: 1, pageSize: 20 }));
                        this.store.dispatch(SpinnerActions.startSpinner());
                    }
                    /* On page refresh groups will not be loaded from the store
                        so we need to fetch that single group from server
                    */
                    if (id && !group && !this.groupLoading) {
                        this.groupLoading = true;
                        this.store.dispatch(GroupActions.getSingleGroup({ groupId: id }));
                    }
                    console.log(loaded, groups, group);

                }),
                filter(([loaded, groups, group]) => {
                    if (id) {
                        return group !== undefined ? true : false;
                    } else {
                        return !!loaded;
                    }
                }),
                map(([loaded, groups, group]) => {
                    if (group) {
                        return true;
                    }
                    // Offer to create a group if the array is empty //
                    if (groups.length === 0) {
                        this.router.navigate(['/groups/create'], {
                            queryParams: {
                                empty: true,
                                topic
                            }
                        });
                        return false;
                    }
                }),
                first(),
                finalize(() => {
                    this.isLoading = false;
                    this.groupLoading = false;
                }));
    }
}
解析(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察{ const topic=route.queryParams['topic']; const id=route.paramMap.get('id'); 把这个还给我 .烟斗( 选择(已加载,{topic}), 最晚从( this.store.pipe(select(selectTopicGroups,{topic})), this.store.pipe(select(selectGroupById,{id})) ), 点击([已加载,组,组])=>{ 如果(!loaded&!this.isLoading){ this.isLoading=true; this.store.dispatch(GroupActions.getTopicGroups({topic,pageNumber:1,pageSize:20})); this.store.dispatch(spinnerations.startSpinner()); } /*不会从存储中加载页内刷新组 所以,我们需要从服务器获取单个组 */ if(id&&!group&&!this.groupLoading){ this.groupload=true; this.store.dispatch(GroupActions.getSingleGroup({groupId:id})); } console.log(已加载、组、组); }), 筛选器(([已加载,组,组])=>{ 如果(id){ 返回组!==未定义?真:假; }否则{ 返回!!已加载; } }), 映射(([已加载,组,组])=>{ 国际单项体育联合会(小组){ 返回true; } //如果阵列为空,则提供创建组// 如果(groups.length==0){ this.router.navigate(['/groups/create']{ 查询参数:{ 空:是的, 话题 } }); 返回false; } }), 第一个(), 完成(()=>{ this.isLoading=false; this.groupload=false; })); } } 选择器:

export const selectGroupsState = createFeatureSelector<GroupState>('groups');

export const selectAllGroups = createSelector(
    selectGroupsState,
    fromGroups.selectAll
);

export const selectTopicGroups = createSelector(
    selectAllGroups,
    (groups, props) => {
        const topic = props.topic;
        return groups.filter(g => g.topic === topic);
    }
);

export const isTopicLoaded = createSelector(
    selectGroupsState,
    (state, props) => {
        const topic = props.topic;
        return !!state.topicsLoaded.find(g => g.topic === topic);
    }
);

export const selectGroupById = createSelector(
    selectAllGroups,
    (groups, props) => {
        console.log(groups, props);
        const id = props.id;
        return groups.find(g => g._id === id);
    }
);

export const totalGroups = createSelector(
    selectGroupsState,
    (state, props) => {
        const topic = props.topic;
        const group = state.topicsLoaded.find(g => g.topic === topic);
        return group ? group.count : 0;
    }
);

export const groupsAlreadyLoaded = createSelector(
    selectGroupsState,
    (state, props) => {
        const topic = props.topic;
        const group = state.topicsLoaded.find(g => g.topic === topic);
        return group ? group.groupsLoaded : 0;
    }
);
导出常量selectGroupsState=createFeatureSelector(“组”); export const selectAllGroups=createSelector( 选择GroupsState, 从groups.selectAll ); export const selectTopicGroups=createSelector( 选择所有组, (组、道具)=>{ const topic=props.topic; 返回groups.filter(g=>g.topic==topic); } ); export const isTopicLoaded=createSelector( 选择GroupsState, (状态、道具)=>{ const topic=props.topic; return!!state.topicsloadded.find(g=>g.topic==topic); } ); export const selectGroupById=createSelector( 选择所有组, (组、道具)=>{ 控制台日志(组、道具); const id=props.id; 返回组.find(g=>g.\u id==id); } ); export const totalGroups=createSelector( 选择GroupsState, (状态、道具)=>{ const topic=props.topic; const group=state.topicsloadded.find(g=>g.topic==topic); 返回组?组计数:0; } ); export const groupsReadyLoaded=createSelector( 选择GroupsState, (状态、道具)=>{ const topic=props.topic; const group=state.topicsloadded.find(g=>