vue-cli3.0 根据环境自动获取相应接口地址

在项目根目录创建config文件夹,然后创建2个文件
创建文件
pro.env.js:

module.exports = {
    NODE_ENV: '"production"', // 生产环境
    API_ROOT: '""' // 填上自己的接口的网址
}

dev.env.js:

module.exports = {
    NODE_ENV: '"development"', // 开发环境
    API_ROOT: '""' // 填上自己的接口的网址
}

在vue.config.js文件中(没有就在根目录自己创建):

// 根据环境自动获取对应接口地址
const proEnv = require('./config/pro.env')
const devEnv = require('./config/dev.env')
const env = process.env.NODE_ENV
let target = ''
if (env === 'production') {
    target = proEnv.API_ROOT
} else {
    target = devEnv.API_ROOT
}

module.exports = {
	devServer: {
        host: '0.0.0.0',
        port: 8086,
        proxy: {
            '/apis': {
                target: target, // 接口网址
                changeOrigin: true, // 是否跨域
                pathRewrite: {
                    '^/apis': ''
                }
            }
        }
    }
}

你可能感兴趣的:(vue)