一、配置
import Axios from "axios";
Axios.get("/getuser") ||.post("/gettable")
.then((res) => {
console.log(res.data.result);
})
.catch((error) => {
console.log(error);
})
.finally(() => {
console.log("最后执行操作");
});
二、注意点
写ECMAScript 2015 + ,async可以写的 ,只不过编译async await 报错。
需要将es6转换为es5。
"babel-plugin-transform-runtime": "^6.23.0","babel-polyfill": "^6.26.0",
去babel文件配置插件名称,配置完成之后重启项目。
async 函数 异步
async function getaxios() {
// async 可以写多个异步等待
try {
let user = await Axios.get("/getuser");
return user;
}
catch(e)
{
throw e;
}
finally {
}
}
getaxios()
.then(res => {
console.log(res.data.result);
})
.catch(err => {
console.log(err);
})
.finally(() => {
console.log("最终执行");
});
全局请求数据,需在main.js配置 Vue.prototype.$axios=Axios;
// 在mainjs里面配置 axios 全局引入
this.$axios.get("/getuser").then((res)=>{
console.log(res.data.result);
}).catch((err)=>{
console.log(err);
});