原創文章,做者: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後端