1.在src目录下新建api文件夹,新建js文件
2.封装axios接口
import axios from "axios";
import router from "../router";
import {Dialog} from "vant";
//请求拦截器
let instance = axios.create({
baseURL:"https://baby.xmn.top", //接口所在域名,将会和传递过来的url进行拼接
timeOut:5000 //过期时间
});
//设置请求拦截
instance.interceptors.request.use(config => {
//判断是否存在token,如果存在将每个页面的header都添加token
if(localStorage.getItem(sessionStorage.getItem("id")+'_admin_token')){
config.headers.common['XX-Device-Type']="mobile";
config.headers.common['XX-Token']=localStorage.getItem(sessionStorage.getItem("id")+'_admin_token')
}
return config;
}, error => {
// 请求错误
return Prom