[Vuejs]-Can a variable in data be computed based on other data?

3πŸ‘

βœ…

The solution is to use computed values, b will be accessible the same way as if it was declared in data:

new Vue({
  el: '#root',
  data: {
    a: 1
  },
  computed: {
    // a computed getter
    b: function() {
      // `this` points to the vm instance
      return this.a + 1
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>

<div id="root">
  {{ a }} and {{ b }}
</div>
πŸ‘€WoJ

-1πŸ‘

try this

var x;
new Vue(x = {
   el: '#root',
   data: {
     a: 1,
     b: () =>  x.data.a + 1
   }
})
πŸ‘€Semi-Friends

Leave a comment