用axios的get请求了数据 怎么取出data中的数据
先在data定义一个变量
data () {
return {
name: ''
}
},
created () {
axios.get(url).then(res => {
this.name = res.data.name
})
},
<script>
import axios from 'axios'
export default {
name: 'App',
data() {
return {
}
},
// 发送ajax请求,response返回响应,response.data拿到数据,之后要放入vuex的state中,
// 然后自定义了一个变量存储allDatas,然后各个组件都可以拿到他通过this.$store.state.allDatas;
mounted() {
console.log(this)
var _this = this
axios.get('http://127.0.0.1:8080/api/comments/')
.then(function(response) {
// console.log(response.data);
console.log(this)
_this.$store.state.allDatas = response.data;
})
.catch(function(error) {
console.log(error);
});
}
}
</script>
axios.get(url).then(res => {
console.log(res)
this.myName = res.data.name
})
请求成功先打印一下res,看看数据结构,一般会写拦截器,直接返回res.data, 所以获取就成了res.name,具体就看你数据结构把
如有帮助,麻烦点个【采纳此答案】 谢谢啦~