在vue.config.js 配置文件中添加
module.exports = {
devServer: {
host: 'localhost', // 本地的域名
post: 8080, // 本地网站的端口
proxy: {
// 会拦截所有/api开头的请求
'/api': {
target: 'http://localhost:3000', // 要跨域的域名
changeOrigin: true // 是否开启跨域
}
}
}
}
为axios添加请求头
//为请求头添加token
axios.interceptors.request.use(config => {
// console.log(config)
config.headers.Authorization = window.sessionStorage.getItem('token')
// 在最后必须 return config
return config
})