1.安装axios 

 

  • npm install axios 
npm安装时出现run `npm audit fix` to fix them, or `npm audit` for details

npm audit fix
npm audit fix --force
npm audit
  • npm install --save axios vue-axios

用到post请求时,需要安装qs

  • import qs from 'qs'

2.在main.js引入axios

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

Vue.prototype.axios = axios;
Vue.use(VueAxios, axios);

3.在页面js处引入
 

import axios from 'axios'
import qs from 'qs'

4.vue-axios使用

// 组件中使用get方法
  axios({ method: "get",
                        url: url,
                        params: {
                          name: this.name,
                          password: this.password,          
                          }
                        }                                  
            ).then((response) => {
                console.log(response)
            }).catch((error) => {
                console.log(error)
            }) 


// 组件中使用post方法
        var user={name: this.name,
            password: this.password};     
            
          axios(
          {
                        method: "POST",
                        url: url,
                        data:user,
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8',  //指定消息格式
                        },
                    }
           )
          .then(res => {
                    console.log("111")
          }, res => {
            // 错误回调
                    console.log("222")
          })
     

5.后台接受用@RequestBody

Logo

前往低代码交流专区

更多推荐