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

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

在开发过程中,前端请求访问的是自己本机启动的后台服务,此时涉及到跨域(因为端口不一样),所以在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进行跨域的转发配置。

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

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容