Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

How can I set the value of name to the property personOne in object? So that name will have the value of Alex.

var app = new Vue({
  el: '#app',
  data: {
    name: '',
    object: { "personOne": "Alex", "personTwo": "Jack"}
  }
})

share|improve this question
    
Can you give us an actual, real-usage example? Seems fairly clear the real scenario isn't as hard-coded, or you'd just do name: 'Alex' and be done with it. Are you loading object via AJAX or something? If so, a computed property is probably reasonable. – ceejayoz Jan 22 at 3:01
    
BTW this is not valid javascript object - it should be key -> value - personOne: 'Alex' – Belmin Bedak 2 days ago

You can use one of the life-cycle hooks like created created or mounted for setting initial data, loading data from API, etc, like following:

var app = new Vue({
  el: '#app',
  data: {
    name: '',
    object: { "personOne": "Alex", "personTwo": "Jack"}
  },
  methods: {
    setName (name) {
      this.name = name
    }
  },
  mounted () {
     this.serName(this.object.personOne)
  },
})
share|improve this answer
    
This seems a bit overcomplicated when a simple computed property would suffice. – ceejayoz Jan 22 at 3:03
    
@ceejayoz I don't think OP just wants a computed property as in that case he can just access this.object.personOne. – Saurabh Jan 22 at 9:22
    
That's why I'm thinking OP has something else going on that they're not explaining well. – ceejayoz 2 days ago

From within the Vue object you write

this.name = 'Alex' and outside you write app.name = 'Alex'

app.someDataField will change the data property called someDataField

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.