[Solved] Computed property “xxx” was assigned to but it has no setter.

When vue uses the computed property to calculate certain values, when reassigning the value on the page, the error Computed property “xxxx” was assigned to but it has no setter is reported. Translated into Chinese, it means “calculated property “xxxx” has been assigned, but it has no setter. “, understand, that the attribute xxxx gets the value, but there is no way to set the value. Post the error code example:

computed: {
  cityOptions () {
    return  this .$store.state.common.cityOptions
  }
},
created () {
  this .getCityOptions()
},
methods : {
  getCityOptions () {
    getCitys().then( res => {
      this .cityOptions = res.data
    })
  }
}

At this point, our code needs to be modified to:

computed: {
  cityOptions: {
    get () {
      return  this .$store.state.common.cityOptions
    },
    set ( val ) {
      return  val
    }
  }
},
created() {
  this .getCityOptions()
},
methods: {
  getCityOptions() {
    getCitys().then(res => {
      this .cityOptions = res. data
    })
  }
}

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *