尝试将我的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); …Run Code Online (Sandbox Code Playgroud)