在项目中api管理需要用到全局变量,创建全局变量的方式也有很多。
1.通过export default
  const BASEURL = "http://localhost:3333/"
  const URL = {
      getCategory:BASEURL+'category',
      getGoodsInfo:BASEURL+'getGoodsInfo'
  }
  export default URL
  在入口文件中引入
import url from './api/api'
Vue.prototype.URL=url;
2.通过module.exports
const BASEURL = "http://localhost:3333/"
const URL = {
  getCategory:BASEURL+'category',
  getGoodsInfo:BASEURL+'getGoodsInfo'
}
module.exports = URL;
在入口文件中引入Vue.prototype.URL=require('./api/api.js');
其实以上两种都是Vue.prototype原型引入;
3.通过node.js的global全局变量
global.apiBase={}
 apiBase.baseUrl='http://localhost:3333/'
apiBase.getBanner="/banners"
 apiBase.getcategory="/category"
export default {
    apiBase
};
在路口文件中直接引入
import ApiBase from './api/api'
4.通过VUEX的状态管理
import Vue from 'vue';
import Vuex from 'vuex';Vue.use(Vuex);
const state = {}
直接定义在state状态里面
5.通过window对象设置
                    
                
                
            
        
浙公网安备 33010602011771号