原创文章,作者:stark,如若转载,请注明出处:https://shudong.wang/blog/61dbd68c20720
proxyTable: {
'/goods/*': {
target: 'http://localhost:3000'
},
'/users/*': {
target: 'http://localhost:3000'
}
},
这种配置方式在一定程度上解决了跨域问题,但是会带来一些问题,
比如我们的vue 路由 也命名为 goods,这时候就会产生了冲突,
如果项目中接口很多,都在这里配置是很麻烦的,也容易产生路由冲突。
把以上配置统一前面加上 /api/
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);
})
}
在入口文件里面配置如下:
import Axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, Axios)
Axios.defaults.baseURL = 'api'
如果这配置 'api/' 会默认读取本地的域
在config 文件夹里面新建一个 api.config.js 配置文件
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 配置文件
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项目上线 看看这个文章,专门讲上线的