【问题标题】:Using Axios and Vue to fetch api data - returning undefined使用 Axios 和 Vue 获取 api 数据 - 返回 undefined
【发布时间】:2018-09-03 05:50:04
【问题描述】:

在尝试将我的 API 与 Vue/Axios 集成时遇到了麻烦。基本上,Axios 正在获取数据(它确实 console.log 我想要的)......但是当我尝试将该数据获取到我的空变量(在我的组件的数据对象中)来存储它时,它会抛出一个“未定义在评估”错误。关于为什么这对我不起作用的任何想法?谢谢!

<template>
  <div class="wallet-container">
    <h1 class="title">{{ title }}</h1>
    <div class="row">
      {{ thoughtWallet }}
    </div>
  </div>
</template>

<script>

import axios from 'axios';
export default {

  name: 'ThoughtWallet',
  data () {
    return {
      title: 'My ThoughtWallet',
      thoughtWallet: [],
    }
  },
  created: function() {
    this.loadThoughtWallet();
  },
  methods: {
    loadThoughtWallet: function() {
      this.thoughtWallet[0] = 'Loading...',
      axios.get('http://localhost:3000/api/thoughts').then(function(response) {
        console.log(response.data); // DISPLAYS THE DATA I WANT
        this.thoughtWallet = response.data; // THROWS TYPE ERROR: Cannot set property 'thoughtWallet' of undefined at eval
      }).catch(function(error) {
        console.log(error);
      });
    }
  }
}

</script>

【问题讨论】:

  • 我认为问题在于您正在尝试打印对象..

标签: javascript vuejs2 axios


【解决方案1】:

因为你使用.then(function(..) { })this不会引用vue上下文this

您有两种解决方案,一种是在axios调用之前设置一个引用您想要的this的变量,例如:

var that = this.thoughtWallet
axios.get('http://localhost:3000/api/thoughts').then(function(response) {
        console.log(response.data); // DISPLAYS THE DATA I WANT
        that = response.data; // THROWS TYPE ERROR: Cannot set property 'thoughtWallet' of undefined at eval
      }).catch(function(error) {
        console.log(error);
      });

另一个是使用新的语法(你需要确保你的代码对于还不支持它的浏览器正确地转译),它允许你在 axios 的作用域内访问this那么。

axios.get('http://localhost:3000/api/thoughts').then((response) => {
        console.log(response.data); // DISPLAYS THE DATA I WANT
        this.thoughtWallet = response.data; // THROWS TYPE ERROR: Cannot set property 'thoughtWallet' of undefined at eval
      }).catch(function(error) {
        console.log(error);
      });

发生这种情况的原因是因为在该函数内部,this 将引用函数的上下文,因此不会有 thoughtWallet 属性

【讨论】:

  • 非常有帮助的解释。谢谢!
  • The reason this happens is because inside that function/then, this will be referring to the context of the function, hence there won't be a thoughtWallet property. - >不幸的是,实际上有数百个 Vue + axios 教程教我们通过在 get 或 post 方法中执行 this.variable 从 axios 传递结果
【解决方案2】:

.get 方法中的this.thoughtWallet 指的是 axios 对象,而不是 Vue 的。您可以简单地在开始时定义 Vue 的this

methods: {
  loadThoughtWallet: function() {
    let self = this;

    this.thoughtWallet[0] = 'Loading...',
    axios.get('http://localhost:3000/api/thoughts').then(function(response) {
      console.log(response.data); // DISPLAYS THE DATA I WANT

      self.thoughtWallet = response.data;

    }).catch(function(error) {
      console.log(error);
    });
  }
}

【讨论】:

  • this.thoughtWallet inside the .get method is referring to the axios object, not Vue's. - >不幸的是,实际上有数百个 Vue + axios 教程教我们通过在 get 或 post 方法中执行 this.variable 从 axios 传递结果
猜你喜欢
  • 2019-06-20
  • 2021-03-07
  • 2021-09-22
  • 2021-05-26
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-27
  • 2021-09-20
相关资源
最近更新 更多