0

var vm = new Vue({
  el: '#app',
  data () {
    return {
      info: null
    }
  },
  mounted () {
    axios
      .get('https://api.coindesk.com/v1/bpi/currentprice.json')
      .then(response => (this.info = response))
  }
})

console.log(vm.info)
//Why vm.info is null? I think it has already been overrided.
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id="app"></div>

Why vm.info is null? I think it has already been overrided.

https://v2.vuejs.org/v2/guide/

When I only use vue.js, this can be overrided.

How can I get the updated vm.info?

1 Answer 1

4

Because axios.get is an asynchronous method, console.log is executed before the info is updated; To check the updated info, you'll have to chain a then method after axios.get(...), something like axios.get(...).then(...).then(() => console.log(this.info)).

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

2 Comments

You'll have to call that function after axios. For instance, axios.get.then().then(myFunctionToExecute).
I guess 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.