在項目中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對象設置