0

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"}
  }
})

2
  • 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. Commented Jan 22, 2017 at 3:01
  • BTW this is not valid javascript object - it should be key -> value - personOne: 'Alex' Commented Jan 22, 2017 at 11:57

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.setName(this.object.personOne)
  },
})

3 Comments

This seems a bit overcomplicated when a simple computed property would suffice.
@ceejayoz I don't think OP just wants a computed property as in that case he can just access this.object.personOne.
That's why I'm thinking OP has something else going on that they're not explaining well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.