在 NGXS 中的其他选择器中重用选择器

Reuse selector in other selectors in NGXS

我有两个 类 PizzasState 和 ToppingsState。 PizzaState 已经有选择器来选择比萨。

@State<PizzaStateModel>({
  name: 'pizzas',
  defaults: initialState
})
export class PizzasState {
  constructor(private pizzaService: PizzasService) {
  }

  @Selector([RouterState])
  static getSelectedPizza(
    state: PizzaStateModel,
    routerState: RouterStateModel<RouterStateParams>
  ): Pizza {
    const pizzaId = routerState.state && routerState.state.params.pizzaId;
    return pizzaId && state.entities[pizzaId];
  }

  @Selector()
  getPizzaVisualized(state: PizzaStateModel): Pizza {
    //
    // what is here?
    //
  }
}

并且 ToppingsState 已选择 Toppings

@State({
  name: 'toppings',
  defaults: initialState
})
export class ToppingsState {
  constructor(private toppingsService: ToppingsService) {
  }

  @Selector()
  static selectedToppings(state: ToppingsStateModel): number[] {
    return state.selectedToppings;
  }

现在我想加入我选择的比萨和选择的配料,并得到我的可视化比萨。

如何正确重用 getSelectedPizza 和 getSelectedToppings? 谢谢

看来您需要使用 joining selector

@Selector([ToppingsState.selectedToppings])
getPizzaVisualized(state: PizzaStateModel, selectedToppings: number[]): Pizza {
    // User data from both states.
}