您的位置:首页 > 产品设计 > UI/UE

在vuecli中使用axios请求发送成功,获取不到返回值的问题

2018-03-09 12:36 806 查看
首先我先描述一下我遇到的问题,以便读者对症下药:

在vuecli中使用axios,请求成功,network也能看见返回的值,但是进入.then(res)中时,res时undefined,原代码如下:

axios.post('http://localhost:12612/api/register', this.ruleForm)
.then((res) => {  //res为undefined
debugger
console.log(res)
})
.catch((error) => {
console.log(error)
})


查资料说是异步的问题什么的,我现在也不确定,先分享一下解决方案

方案一:axios 改写为 Vue 的原型属性

在main.js中
Vue.prototype.$http = axios


使用

this.$http.post('http://localhost:12612/api/register', this.ruleForm)
.then((res) => {  //res为undefined
debugger
console.log(res)
})
.catch((error) => {
console.log(error)
})


方案二:结合vue-axios使用

npm install vue-axios --save


在main.js中引入

import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios,axios)


使用方法:register.vue

import axios from 'axios'
import vueAxios from 'vue-axios'
.
.
.
this.axios.post('http://localhost:12612/api/register', this.ruleForm)
then((res) => {
console.log(res)
})
.catch((error) => {
console.log(error)
})


方案三:结合vuex使用

from
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: