【问题标题】:How to get updated vm after use vue.js and axios?使用 vue.js 和 axios 后如何获取更新的 vm?
【发布时间】:2018-10-09 18:25:06
【问题描述】:

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>

为什么 vm.info 为空?我认为它已经被覆盖了。

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

当我只使用 vue.js 时,这个可以被覆盖。

如何获取更新后的 vm.info?

【问题讨论】:

    标签: vue.js vuejs2 axios


    【解决方案1】:

    因为axios.get是异步方法,所以console.log在信息更新之前执行;要检查更新的信息,您必须在 axios.get(...) 之后链接 then 方法,例如 axios.get(...).then(...).then(() =&gt; console.log(this.info))

    【讨论】:

    • 您必须在axios 之后调用该函数。例如,axios.get.then().then(myFunctionToExecute)
    • 我猜。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 2020-09-27
    • 1970-01-01
    • 2020-07-17
    • 2019-10-15
    • 2017-11-06
    • 2021-11-17
    • 2018-09-08
    相关资源
    最近更新 更多