gpt4 book ai didi

ngxs:在 Action 中访问不同的状态

转载 作者:行者123 更新时间:2023-12-04 14:22:43 26 4
gpt4 key购买 nike

是否可以在操作中访问不同的状态?

设想:
我有两种状态:

  • 过滤状态
  • 应用状态
  • FilterState包含一个 Action Filter , 当过滤器 Action 被触发时, filterService使用操作的有效负载 + 来自 AppState 的值调用.
    @Action(Filter)
    filter(ctx, action) {
    // HOW TO GET VALUE FROM AppState

    return this.filterService.filter(action, valueFromOtherStore).pipe(
    tap(data => {
    // Do something with result
    })
    );
    }

    如何从不同状态检索值以将此值应用于 this.filterService.filter 的第二个参数?

    最佳答案

    Shared State文档回答了这个问题:

  • 文档:https://ngxs.gitbook.io/ngxs/advanced/shared-state

  • 访问不同的状态 selectSnapshot方法在 store可以使用: this.store.selectSnapshot(PreferencesState.getSort)
    示例
    @State<PreferencesStateModel>({
    name: 'preferences',
    defaults: {
    sort: [{ prop: 'name', dir: 'asc' }]
    }
    })
    export class PreferencesState {
    @Selector()
    static getSort(state: PreferencesStateModel) {
    return state.sort;
    }
    }

    @State<AnimalStateModel>({
    name: 'animals',
    defaults: [
    animals: []
    ]
    })
    export class AnimalState {

    constructor(private store: Store) {}

    @Action(GetAnimals)
    getAnimals(ctx: StateContext<AnimalStateModel>) {
    const state = ctx.getState();

    // select the snapshot state from preferences
    const sort = this.store.selectSnapshot(PreferencesState.getSort);

    // do sort magic here
    return state.sort(sort);
    }

    }

    关于ngxs:在 Action 中访问不同的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358636/

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