服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - vue 接口请求地址前缀本地开发和线上开发设置方式

vue 接口请求地址前缀本地开发和线上开发设置方式

2021-08-21 17:45微微一芯 JavaScript

这篇文章主要介绍了vue 接口请求地址前缀本地开发和线上开发设置方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

开发环境 config/dev.env.js

?
1
2
3
4
5
6
7
8
'use strict'
const merge = require('webpack-merge')
const prodenv = require('./dev.env')
 
module.exports = merge(prodenv, {
 node_env: '"development"',
 api_root: '"https://www.dev.com"' //本地请求前缀
})

线上开发环境 config/prod.env.js

?
1
2
3
4
5
6
7
8
'use strict'
const merge = require('webpack-merge')
const prodenv = require('./prod.env')
 
module.exports = merge(prodenv, {
 node_env: '"production"',
 api_root: '"https://www.prov.com"'  //线上请求前缀
})

在请求之前,组装url,axios.js

?
1
2
3
4
5
6
7
8
import axios from 'axios';
var root = process.env.api_root;
//请求拦截
axios.interceptors.request.use((config) => {
  //请求之前重新拼装url
  config.url = root + config.url;
  return config;
});

页面使用模板:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export default {
  name: 'order',
  data () {
    return {
      order_list: []
    }
  },
  methods: {
    fetchlist: function () {
      this.$axios.post('/api/order_list').then((res) => {
        if(res.result === '0000'){
          this.order_list = res.data;
        }
      });
    }
  }
}

补充知识:vue中axios固定url请求前缀

vue 接口请求地址前缀本地开发和线上开发设置方式

main.js中添加:

vue 接口请求地址前缀本地开发和线上开发设置方式

使用方法:

vue 接口请求地址前缀本地开发和线上开发设置方式

以上这篇vue 接口请求地址前缀本地开发和线上开发设置方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wuyan1001/article/details/84840703

延伸 · 阅读

精彩推荐