一,在入口文件引入axios并配置请求头根路径和请求头拦截
//引入axios
import axios from'axios'
//设置请求根路径
axios.defaults.baseUrl='http://127.0.0.0:8001/api/private/v1/'
//设置请求头拦截
axios.interceptors.request.use(config= >{
config.headers.Authorization = window.sessionStorage.getItem('token');
console.log(config);
// 在最后必须return config
return config
})
// 把axios 挂载在VUe实例对象上
Vue.prototype.$http =axios
经过以上设置则可以通过 this.$http发送axios请求后端接口
二,发送各类请求携带参数的格式:
get请求
const { data :res} =await this.$http.get(`categories/${this.cateId}/attributes`,
{ params:{sel: this.activeName}}
);
//若要求参数携带在URL中,可以通过``包裹后端地址,使用${}在URL中携带对应参数,
//不携带在url的参数需要通过params传递参数
//若参数不携带在url中,且参数很多可以把接口所需要参数打包成一个对象,通过params把由各个参数组成对象直接给后端接口