0

I'd like to have a data prop populated by data received from an axios http request. Here's a simplified version of the issue.

I would like the 'hello' prop to populate when the app loads. And when the user clicks the button, the 'hello' prop should update. The click-and-update part is working as expected, but how do I get the data to populate the 'hello' prop initially? I've also tried it with computed properties, but that also doesn't populate the 'hello' prop on the initial load. Thanks!

<div id="app">
  <h1>{{ hello }}</h1>
  <button @click="updateText()">Update text!</button>
</div>

var app = new Vue({
  el:'#app',
  created() { 
    updateText() // Calling this here does not work
  },
  data: {
    hello: ''
  },
  methods: {
    updateText: function() {
      this.hello = 'hello'
    }
  }
});
0

1 Answer 1

1

You don't have this. before the method name inside created so it's not calling the method:

var app = new Vue({
  el: '#app',
  data: function() {
    return {
      hello: ''
    }
  },
  created () { 
    this.updateText()
  },
  methods: {
    updateText: function() {
      this.hello = 'hello'
    }
  }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Duh, that did it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.