您的位置:首页 > Web前端 > Vue.js

详解vue+axios给开发环境和生产环境配置不同的接口地址

2019-10-30 07:05 1311 查看

为什么要配置不同的接口地址

在开发过程中,前端请求访问的是自己本机启动的后台服务,此时涉及到跨域(因为端口不一样),所以在config/index.js文件中配置了代理

//检查某个文件是否存在
try {
fs.statSync(path.join(__dirname, '../proxy/' + templatedir + '.json'))
//如果可以执行到这里那么就表示存在了
console.log(124)
proxyTable = require('../proxy/' + templatedir + '.json')
} catch (e) {
//捕获异常'
}
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: proxyTable,

// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
index: templatedir,
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
}

然后再proxy文件夹里建立对应的项目名.json文件,

{
"/": {
"target": "", // 被请求的地址
"changeOrigin": true,
"pathRewrite": {
"^/": "/"
}
}
}

注意:proxyTanle这个插件只限于开发模式下,后面一定要将前端代码和服务器代码部署在一起,负责需要通过Nginx进行跨域的转发配置。

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐