3

我正在使用来自 Vuex 4 的 Vue 3 组合 API 和 useStore 挂钩

这就是我从 setup() 内的 vuex 存储中获取数据(id)的方式

    setup () {
                const store = useStore()    
                const allIds = computed(() => store.state.ids)               
    
            function printIds () {
                console.log(allIds.value)
               }
             //...
         }

问题是我无法使用allIds数组,因为它包含在代理中。这就是我在 console.log(allIds.value) 上得到的 -

在此处输入图像描述

当我在模板中使用此数组时,它工作正常,但我想将此数据用作组件内部另一个动作调度的有效负载,使用数组中的 1 个 id。我怎样才能做到这一点?我发现的所有示例都是在模板中使用商店数据。

谢谢。

4

1 回答 1

2

代理不会阻止您将数据用作有效负载,您只需要使用该.value属性并从那里正确访问数据。

变量是一个对象,其属性allIds中有一个数组。pads所以要传递数组中的第一项,例如:

const store = useStore()    
const allIds = computed(() => store.state.ids)               
    
store.dispatch('actionName', allIds.value.pads[0]);

店铺

actions: {
  actionName({ commit }, payload) {
    console.log(payload);
  }
}
于 2021-02-21T11:31:30.323 回答