[Vuejs]-Vue use computed property in data

2👍

data() is invoked before computed props are resolved, so you can’t reference computed props from there.

I assume this question is related to your other question, where form needs to be used in v-model. Since form depends on the computed prop, you should use a watcher on the computed prop that updates form accordingly:

export default {
  data() {
    return {
      form: []
    }
  },
  computed: {
    properties() {/*...*/},
  },
  watch: {
    properties(properties) {
      this.form = properties.map(_ => ({ property_type: [] }))
    }
  }
}

1👍

What is const properties = ['Property 1', 'Property 2'] ?

Pretty sure you cannot have it nested inside of data. Move it to the upper scope maybe or define it directly like

data() {
  return {
    properties: ['Property 1', 'Property 2']
  }
}

Also, it’s not very clear as of what you’re trying to achieve here. A lot of things are mixed together. You’re not really supposed to make a map in data since it’s here for raw data (as it’s called).

Here is the official documentation on computed properties, maybe give it a second read or update your question by explaining a bit better what is happening here.

Leave a comment