How to put a value of data object in another data object vueJS

后端 未结 1 2046
无人及你
无人及你 2020-12-14 21:29

Here is my code:

data () {
  return {
    msg: \'\',
    rgbValue: \'\',
    newColor: {
      color: this.msg
    }
         


        
相关标签:
1条回答
  • 2020-12-14 22:11

    You won't be able to access data properties like this.msg until the data method has returned.

    Just set that value outside of the return statement:

    data () {
      let msg = '';
    
      return {
        msg: msg,
        rgbValue: '',
        newColor: {
          color: msg
        }
      }
    }
    

    If you need the newColor property to always reflect the value of this.msg, you could make it a computed property instead:

    data () {
      return {
        msg: '',
        rgbValue: '',
      }
    },
    computed: {
      newColor() {
        return { color: this.msg }
      }
    }
    
    0 讨论(0)
提交回复
热议问题