NGXS-自定义Select()

Jos*_*ose 2 select ngxs

我正在尝试创建一个自定义@Select(),以便能够向下钻取特定树并动态返回值。获取未定义或未定义(正在执行)

user.component.ts

const location = 'new york'
@Select(state => UserState.getUserLocationSlots(state, location)) slots$;
Run Code Online (Sandbox Code Playgroud)

user.state.ts

@Selector() 
static getUserLocationSlots(state: UserStateModel, location: any) {
  console.log(state);
  console.log(location); // <-- expecting 'new york', but getting undefined
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以通过使用实现这个crateSelector功能,从@ngxs/store

在您的.state.ts文件中:

static getLocationSlots(location: string) {
    return createSelector([UserState], (state: string[) => {
        // logic for filtering your data
        // eg.: state.filter(element => element == location)
    })
}

In your .component.ts file:

@Select(UserState.getLocationSlots('new york')) slots$: Observable<any>
Run Code Online (Sandbox Code Playgroud)

您也可以在这里查看更多详细信息


kct*_*ang 4

我认为不可能将参数传递给@Selector()ngxs v2 中的修饰函数。不过那会很好。

此功能请求存在一张票证。

另外,我认为您没有正确使用 @Selector() 。我应该是这样的(因此,不能传递参数):

@Select(UserState.getUserLocationSlots) slots$
Run Code Online (Sandbox Code Playgroud)

请参阅文档

注意:我不是 ngxs 方面的专家...这只是基于我现在的理解。