Open qiuhongbingo opened 4 years ago
本文以 vue-cli 项目作为 demo 示例
借助 proxy 代理服务器地址,这种方式的优缺
配置方法
vue.config.js
module.exports = { devServer: { proxy: { '/api/v1': { target: 'https://api1.com', changeOrigin: true } } } }
methods: { async fetch() { await this.$axios.get('/api/v1/xxx') } }
server { location /api/v1 { proxy_pass https://api2.com; } }
借助 env 定义并直连服务器地址,这种方式的优缺
首先定义好需要的环境文件,一般都是三个环境
NODE_ENV = development VUE_APP_API = 'https://beta.api1.com/api/v1'
NODE_ENV = production VUE_APP_API = 'https://prod.api1.com/api/v1'
staging 这里我用的 production 来构建打包,然后连的 beta 服务器地址
NODE_ENV = production VUE_APP_API = 'https://beta.api1.com/api/v1'
methods: { async fetch() { await this.$axios.get(process.env.VUE_APP_API + '/xxx') } }
启动命令可以增加个 build-beta 方便执行
"scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "build-beta": "vue-cli-service build --mode staging" }
方法一
借助 proxy 代理服务器地址,这种方式的优缺
配置方法
vue.config.js
方法二
借助 env 定义并直连服务器地址,这种方式的优缺
配置方法
首先定义好需要的环境文件,一般都是三个环境
staging 这里我用的 production 来构建打包,然后连的 beta 服务器地址
启动命令可以增加个 build-beta 方便执行