原创文章,做者:stark,如若转载,请注明出处:https://shudong.wang/10233.htmlhtml
proxyTable: { '/goods/*': { target: 'http://localhost:3000' }, '/users/*': { target: 'http://localhost:3000' } },
这种配置方式在必定程度上解决了跨域问题,可是会带来一些问题,
好比咱们的vue 路由 也命名为 goods,这时候就会产生了冲突,
若是项目中接口不少,都在这里配置是很麻烦的,也容易产生路由冲突。
把以上配置统一前面加上 /api/vue
proxyTable: { '/api/**': { target: 'http://localhost:3000' }, },
proxyTable: { '/api/**': { target: 'http://localhost:3000', pathRewrite:{ '^/api':'/' } }, },
logout(){ axios.post('/api/users/logout').then(result=>{ let res = result.data; this.nickName = ''; console.log(res); }) }, getGoods(){ axios.post('/api/goods/list').then(result=>{ let res = result.data; this.nickName = ''; console.log(res); }) }
在入口文件里面配置以下:webpack
import Axios from 'axios' import VueAxios from 'vue-axios' Vue.use(VueAxios, Axios) Axios.defaults.baseURL = 'api' 若是这配置 'api/' 会默认读取本地的域
在config 文件夹里面新建一个 api.config.js 配置文件ios
const isPro = Object.is(process.env.NODE_ENV, 'production') module.exports = { baseUrl: isPro ? 'http://www.vnshop.cn/api/' : 'api/' }
import apiConfig from '../config/api.config' import Axios from 'axios' import VueAxios from 'vue-axios' Vue.use(VueAxios, Axios) Axios.defaults.baseURL = apiConfig.baseUrl
logout(){ this.$http.post('/users/logout').then(result=>{ let res = result.data; this.nickName = ''; console.log(res); }) }, getGoods(){ this.$http.post('/goods/list').then(result=>{ let res = result.data; this.nickName = ''; console.log(res); }) }
proxyTable: { '/api/**': { target: 'http://localhost:3000', pathRewrite:{ '^/api':'/' } }, },
在config 文件夹里面新建一个 api.config.js 配置文件web
const isPro = Object.is(process.env.NODE_ENV, 'production') module.exports = { baseUrl: isPro ? 'http://www.vnshop.cn/api/' : 'api/' }
能够去 dev-server.js 里面看配置代码 const webpackConfig = (process.env.NODE_ENV === 'testing' || process.env.NODE_ENV === 'production') ? require('./webpack.prod.conf') : require('./webpack.dev.conf')
import apiConfig from '../config/api.config' import Axios from 'axios' import VueAxios from 'vue-axios' Vue.use(VueAxios, Axios) Axios.defaults.baseURL = apiConfig.baseUrl
logout(){ this.$http.post('/users/logout').then(result=>{ let res = result.data; this.nickName = ''; console.log(res); }) }, getGoods(){ this.$http.post('/goods/list').then(result=>{ let res = result.data; this.nickName = ''; console.log(res); }) }
vue项目上线 看看这个文章,专门讲上线的axios
https://www.yuque.com/rdhub/a...segmentfault
QQ群:274583408后端