You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NgRx带Props的Selector已弃用,如何重构hasCreateCasePermission?

解决NgRx中"Selectors with props are deprecated"错误的重构方案

你遇到的错误是Selectors with props are deprecated,原代码如下:

const getUserProfileState = createFeatureSelector<UserProfileState>(authorizationFeatureKey);

const hasPermission = createSelector(
    getUserProfileState,
    (state: UserProfileState, permission: GQLPermission): boolean => {
        const result =
            state.permissions &&
            state.permissions.some((p) => p && p.resource === permission.resource && p.action === permission.action);
        return !!result;
    }
);

const hasCreateCasePermission = createSelector(getUserProfileState, (state: UserProfileState): boolean =>
    hasPermission.projector(state, {
        resource: PermissionResource.case,
        action: PermissionAction.create,
    })
);

你已经将hasPermission重构为返回选择器的函数:

const hasPermission = (permission: GQLPermission) => createSelector(
    getUserProfileState,
    (state: UserProfileState): boolean => {
        const result =
            state.permissions &&
            state.permissions.some((p) => p && p.resource === permission.resource && p.action === permission.action);
        return !!result;
    }
);

重构hasCreateCasePermission的方案

直接调用hasPermission并传入指定的权限参数即可,不需要再嵌套createSelector:

const hasCreateCasePermission = hasPermission({
    resource: PermissionResource.case,
    action: PermissionAction.create,
});

说明

重构后的hasPermission是一个工厂函数,接收权限参数后返回一个完整的NgRx选择器。hasCreateCasePermission本质上就是这个工厂函数生成的、针对特定权限的选择器,直接调用工厂函数并传入目标权限就能得到对应的选择器,无需额外包装createSelector。

内容的提问来源于stack exchange,提问作者Meenu Meena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 13:52:06