config --> index.js文件中配置
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api':{
target: 'https://api.focues.com', //这个路径是我代理到本地tp框架里面
changeOrigin: true, //开启代理
pathRewrite: {
"^/api" : "/"
},
}
},
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8090, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map',
// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,
cssSourceMap: true
},
config --> prod.env.js中配置生产环境地址
'use strict'
module.exports = {
NODE_ENV: '"production"',
API_HOST: '"https://api.focues.com"' // 生产环境地址
}
config --> dev.env.js中配置开发环境地址
'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')
module.exports = merge(prodEnv, {
NODE_ENV: '"development"',
API_HOST: '"/api/"' // 加上这个开发环境就直接去调用代理了
})
总接口文件中配置生产模式下和开发模式下的域名,根据开发需要自行调整。
var severUrl = process.env.NODE_ENV === 'production' ? process.env.API_HOST : '/api';
最后在接口内调用即可。