当前位置:网站首页>Vue: Axios uses this pointer

Vue: Axios uses this pointer

2020-11-07 16:54:25 osc_write

Preface

stay vue in , There are three types of modules that request back-end interfaces :

  • vue-resource
  • axios
  • fetch

We use axios To request the back-end interface .

install

Get into vue Project directory , perform :

npm install axios --save

Use

axios Supported by http The request method is as follows

  • axios.request(config)
  • axios.get(url[, config])
  • axios.delete(url[, config])
  • axios.head(url[, config])
  • axios.options(url[, config])
  • axios.post(url[, data[, config]])
  • axios.put(url[, data[, config]])
  • axios.patch(url[, data[, config]])

This article does not focus on axios Use , Here we have get For example , In the use of axios When the receiving interface returns , You need to use arrows => Symbols get the right this The pointer .

 this.$axios
      .get("/userInfo")
      .then(
        (response) =>
          // handle success          (this.userdata = response.data)      )
      .catch(function (error{        // handle error        console.log(error);
      })
      .then(function ({        // always executed
      });

If you don't want to use the arrow function, you can use the variable to get this The pointer , However, it is suggested to use the above method .

 .$axios
      .get("/userInfo")
      .then(
        function(response){
                )
      .( (error) {                console.log(error);
      })
      .then( () {        
      });

 

Blogger : Test makes money

motto : Focus on testing and Automation , Committed to improving R & D efficiency ; Complete the original accumulation through testing , Financial freedom through reading and managing money .

csdn:https://blog.csdn.net/ccgshigao

Blog Garden :https://www.cnblogs.com/qa-freeroad/

51cto:https://blog.51cto.com/14900374

 


版权声明
本文为[osc_write]所创,转载请带上原文链接,感谢