Home > OS >  vue - data reference another data
vue - data reference another data

Time:10-24

I very new in vue, how can mylist random, can get the same value from somerandom

export default {
  data () {
    return {
        somerandom:"foo",
        mylist:{
            random: this.somerandom
        }
    }
  }
}

CodePudding user response:

You can try with computed property:

new Vue({
  el: '#demo',
  data () {
    return {
        somerandom:"foo",
        mylist:{
            random: ''
        }
    }
  },
  computed: {
    rand() {
      return this.mylist.random = this.somerandom
    }
  }
})

Vue.config.productionTip = false
Vue.config.devtools = false
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
    <p>{{ rand }}</p>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

model init an object with value. can set value by default or run func after computed or mounted


data() {
    return {
      somerandom: '',
      someOther: [],
    }
},
computed: {
    useRandom() {
        this.somerandom = this.rand();
    }
},
methods: {
    rand(){
       return 'random';
    }
}
  • Related