vue-axios基本用法

废话不多说,直接搞事搞事。

首先安装axios:

1):npm install

2):npm install vue-axios --save

3):npm install qs.js --save  //它的作用是能把json格式的直接转成data所需的格式

安装成功后,在main.js页面引用:

import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'


Vue.prototype.$axios = axios    //全局注册,使用方法为:this.$axios
Vue.prototype.qs = qs           //全局注册,使用方法为:this.qs

最后开始使用请求:

<script>
    export default{
        data(){
            return{
                userId:666,
          token:'', } }, created(){
this.$axios({ method:'post', url:'api', data:this.qs.stringify({ //这里是发送给后台的数据 userId:this.userId, token:this.token, }) }).then((response) =>{ //这里使用了ES6的语法 console.log(response) //请求成功返回的数据 }).catch((error) =>{ console.log(error) //请求失败返回的数据 }) } } </script>

本文介绍的是axios的基本用法,详细看官方文档https://github.com/axios/axios

一个程序猿开了一个公众号,喜欢的朋友可以关注一下哦《时间的信箱》

原文地址:https://www.cnblogs.com/silent007/p/8603367.html