vue-element-admin 本身已經在Store中儲存了一些全局變量(比如:name、avatar、roles等等),如果你想增加其他内容可以按如下方法添加。
一、在user.js中添加全局變量
1、打開 src/store/modules/user.js 檔案
2、找到 const state = ,增加你要的全局變量,我這裡加的是 companyId
3、找到 const mutations =,增加變量companyId的設定方法
4、找到 getInfo({ commit, state }),将登陸後查詢到的使用者資訊中的 companyId 存入 store 中。其中 SET_COMPANYID 就是步驟3中增加的方法名稱
到此資料已經放到store中
二、在getters.js中增加擷取全局變量的方法
1、打開 src/store/getters.js 檔案
2、找到 const getters = ,在其中增加擷取變量方法
到此擷取全局變量内容的方法也完成了
三、測試
可以按如下步驟驗證是否添加成功
1、打開 src/views/profile 檔案
2、在 computed: { 中增加get方法,在 getUser() 方法中調用get方法得到資料并 log 拿到的資料
3、運作項目點選profile,檢視console即可看到取得的全局變量