0

We're currently converting from boilerplate VUE 2 to Composition API, and i'm struggling to understand how to rewrite our current computed to support Composition API :

setup() {
    const store = useStore<StoreState>();
    // Question : how do i implement the infoFields into the 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: "Fornavn",
        },
        {
          value: (this as any).getContact.customer.lastName,
          label: "Etternavn",
        },
        ...
        ...
        ];
    },


 <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>
3
  • 1
    The code is not equivalent. If you have a getter, use a getter. data becomes ref or reactive. By using a computed inside data you've lost reactivity. Is this intentional? Commented Sep 14, 2021 at 14:15
  • Probably should be reactive yes.. My normal approach would be using a clean getter, populate the form with data without using the iteration. But.. since the tech-lead want it this way, i'm trying to find a way to do this and keep the reactivtiness within.. any ideas @EstusFlask ? Commented Sep 14, 2021 at 14:30
  • 1
    It's like the answer says, just use a computed. Would be the same in Vue 2 to preserve reactivity, with different syntax. Commented Sep 14, 2021 at 14:34

1 Answer 1

2

Not sure what exactly is the problem, but I think using store.getters in computed should work:

const infoFields = computed(() => {
    return [
        {
          value: store.getters["contacts/getContact"].customer.firstName,
          label: "Fornavn",
        },
        {
          value: store.getters["contacts/getContact"].customer.lastName,
          label: "Etternavn",
        }
    ]
})
Sign up to request clarification or add additional context in comments.

2 Comments

This might be sufficent :) Will try this out Igor. (Have to check on the requirements for reactiveness though...)
it worked :) Thanks for great help and for spending time helping me out Igor Moraru and Estus Flask :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.