vue axios配置


一、终端运行

npm install axios

二、main.js配置

//引入axios
import axios from 'axios'
//axios默认配置
axios.defaults.baseURL="http://localhost:8080"
axios.defaults.timeout=5000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//修改原型链,全局使用axios,这样之后可在每个组件的methods中调用$axios命令完成数据请求
Vue.prototype.$axios=axios 

三、安装qs,终端运行

npm install qs

四、qs配置

Vue.prototype.$qs=qs 

五、post方式使用(post要使用qs)

this.$axios
            .post(
              "/user/login",
              this.$qs.stringify({
                xh: this.xh,
                password: this.$md5(this.password1)
              })
            )
            .then(response => {
              
            })
            .catch(error => {
              
            });

六、get方式使用(get不需要qs)

 this.$axios
          .get("/user/select", {
            params: {
              xh: this.ruleForm.xh
            }
          })
          .then(response => {
           
            } else {
             
            }
          })
          .catch(error => {});
vue