https://cli.vuejs.org/zh/config/#devserver-proxy
在vue.config.js中
### 跨域解决
寻找devserver.proxy
vue官方文档 vue Cli
配置参考里面
module.exports = {
devServer: {
proxy: {
'/rng': { //这里最好有一个 /
target: 'https://suggest.taobao.com/', // 后台接口域名
ws: true, //如果要代理 websockets,配置这个参数
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, //是否跨域
pathRewrite:{
'^/rng':''
}
}
}
}
}
===分割线===
在建立一个axios组件
拦截器,内容下面这个
来自于:
https://www.npmjs.com/package/axios
// Add a request interceptor
//请求拦截器,发起请求前对请求参数做拦截处理
axios.interceptors.request.use(function (config) {
// Do something before request is sent
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
// Add a response interceptor
//响应拦截器 接受到数据的时候对数进行处理
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return response;
}, function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(error);
});
export default axios
==分割线==
import axios from './utils/axios'
在main.js中引入axios
Vue.prototype.$axios = axios
//将axios 挂载到vue的原型上 组件实例继承
网友评论