Vue 项目全局配置文件

参考:https://www.cnblogs.com/fangyinghua/p/9298628.html

前言:在项目中有很多时候需要配置一个全局配置文件,方便维护。

配置方法(一般有最少5种方法):

1. 通过export default

const BASEURL = "http://localhost:3333/"
const URL = {
 getCategory:BASEURL+'category',
 getGoodsInfo:BASEURL+'getGoodsInfo'
}

export default {
  BASEURL,
  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对象设置(自行百度)

总结,其实一般用的最多的是第一种。

posted @ 2022-12-06 22:19  轻风细雨_林木木  阅读(158)  评论(0编辑  收藏  举报