进入vue项目的目录,执行:
npm install axios --save
axios支持的http请求方法如下
本篇不重点介绍axios的使用,这里我们以get为例,在使用axios接收接口返回的时候,需要使用箭头=>符号获取正确的this指针。
this.$axios .get("/userInfo") .then( (response) => // handle success (this.userdata = response.data) ) .catch(function (error) { // handle error console.log(error); }) .then(function () { // always executed });
如果不想用箭头函数可以在外部先用变量获取this指针,但是建议还是使用上面的写法。
let that = this;
this.$axios .get("/userInfo") .then( function(response){
// handle success (that.userdata = response.data) ) .catch(function (error) { // handle error console.log(error); }) .then(function () { // always executed });
博主:测试生财
座右铭:专注测试与自动化,致力提高研发效能;通过测试精进完成原始积累,通过读书理财奔向财务自由。
csdn:https://blog.csdn.net/ccgshigao
原文:https://www.cnblogs.com/qa-freeroad/p/13869426.html