重用 NGRX 信号存储中的计算信号

问题描述 投票:0回答:1

我想在 NGRX 信号存储中重用计算出的信号。

例如:

export const ExampleStore = signalStore(
  withState(initialState),
  withComputed(store => ({
    property1: computed(() => store.example()),
    property2: computed(() => store.property1()),
  }))
);

store.property1()
computed

中不可调用
angular ngrx angular-signals
1个回答
0
投票

我知道有两种模式:

使用第二个

withComputed

export const ExampleStore = signalStore(
  withState(initialState),
  withComputed(store => ({
    property1: computed(() => store.example()),
  })),
  withComputed(store => ({
    property2: computed(() => store.property1()),
  }))
);

同样

withComputed

export const ExampleStore = signalStore(
  withState(initialState),
  withComputed(store => {
    const property1 = computed(() => store.example());
    const property2 = computed(() => property1());
    
    return {property1, property2};
  })
);
© www.soinside.com 2019 - 2024. All rights reserved.