首頁  >  問答  >  主體

使用computed屬性在Vue Composition API的setup()函數中轉換數據

我們目前正在從樣板VUE 2轉換為Composition API,並且我正在努力理解如何重寫我們當前的computed以支援Composition API:

setup() {
    const store = useStore<StoreState>();
    // 问题:我如何将infoFields实现到setup中?
    // const contactSingle = computed(() => store.state.contacts.contactSingle);
    return { contactSingle };
  },
computed: {
    ...mapGetters("contacts", ["getContact"]),
    infoFields(): any {
      return [
        {
          value: (this as any).getContact.customer.firstName,
          label: "名字",
        },
        {
          value: (this as any).getContact.customer.lastName,
          label: "姓氏",
        },
        ...
        ...
        ];
    },


 <v-row>
  <v-col class="pt-0" v-for="(item, i) in infoFields" :key="i + '-field'" cols="12" xs="12" sm="6" md="6" lg="4">
    <BaseSheetField :value="item.value" :label="item.label" />
  </v-col>
</v-row>

P粉959676410P粉959676410277 天前442

全部回覆(1)我來回復

  • P粉068510991

    P粉0685109912024-01-17 10:29:36

    不確定問題具體是什麼,但我認為在計算屬性中使用store.getters應該可以解決:

    const infoFields = computed(() => {
        return [
            {
              value: store.getters["contacts/getContact"].customer.firstName,
              label: "名字",
            },
            {
              value: store.getters["contacts/getContact"].customer.lastName,
              label: "姓氏",
            }
        ]
    })

    回覆
    0
  • 取消回覆