Use computed property on data in Vuejs

How can I use the computed property in the data or emit it over the bus?

I have the following vue instance, but myComputed is always undefined, but computedData is working correctly.

var vm = new Vue({
  data(){
    return{
      myComputed: this.computedData
    }
  },

  computed: {
    computedData(){
      return 'Hello World'
    }
  }
})

      

+3


source to share


1 answer


To keep things as simple as possible, just do the job of an observer, if you don't want to highlight changes for different components, or there are many variables that you want to notify, then you might have to use Vuex or an event bus:



var vm = new Vue({
  data(){
    return{
      myComputed: '',
      computedData: 'Hello World'
    }
  },
  created() {
    this.myComputed = this.computedData;
  },
  watch: {
    computedData() {
      this.myComputed = this.computedData;
    }
  }
});

      

+2


source







All Articles