Heim  >  Fragen und Antworten  >  Hauptteil

Verwenden Sie das berechnete Attribut, um Daten in der Funktion setup() der Vue Composition API zu konvertieren

Wir führen derzeit eine Umstellung von Boilerplate VUE 2 auf die Composition API durch und ich versuche zu verstehen, wie wir unser aktuelles computed umschreiben können, um die Composition API zu unterstützen:

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 Tage vor440

Antworte allen(1)Ich werde antworten

  • 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: "姓氏",
            }
        ]
    })

    Antwort
    0
  • StornierenAntwort