当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 详解vue+axios给开发环境和生产环境配置不同的接口地址

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

2019年09月06日  | 移动技术网IT编程  | 我要评论

大唐悍卒,退休政策,核战8小时

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

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网