vuex實戰(zhàn)

1. 頁面獲取全局配置文件數(shù)據(jù)
登錄頁面底部展示放置在全局配置文件的相關(guān)數(shù)據(jù),如軟件版權(quán)信息等
src/config/index.js與首頁相關(guān)配置信息如下:

/**
* 是否顯示設(shè)置的底部信息
*/
showFooter: true,

/**
* 底部文字,支持html語法
*/
footerTxt: '? 2020 Zhou Yi <a href="http://www.itdecent.cn/u/4fc65de1790b" target="_blank">個人博客</a>',

/**
* 備案號
*/
caseNumber: '渝ICP備18005431號'

src/store/index.js ,vuex存儲

import Vue from 'vue'
import Vuex from 'vuex' //這個才是做數(shù)據(jù)存儲的
import user from './modules/user'
import setting from './modules/setting'

Vue.use(Vuex)
//存儲用戶模塊和設(shè)置模塊
const store = new Vuex.Store({
  modules: {
    user,
    setting
  },
})

export default store

src/store/modules/setting.js,設(shè)置模塊

import Config from '@/config'

const settings = {
  state: {
    showRightPanel: false,
    tagsView: Config.tagsView,
    fixedHeader: Config.fixedHeader,
    sidebarLogo: Config.sidebarLogo,
    settingBtn: Config.settingBtn,
    uniqueOpened: Config.uniqueOpened,
    showFooter: Config.showFooter,
    footerTxt: Config.footerTxt,
    caseNumber: Config.caseNumber
  },
  mutations: {
    CHANGE_SETTING: (state, { key, value }) => {
      if (state.hasOwnProperty(key)) {
        state[key] = value
      }
    }
  },
  actions: {
    changeSetting({ commit }, data) {
      commit('CHANGE_SETTING', data)
    }
  }
}
export default settings

入口main.js中引入store

import store from './store'

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

src/views/login.vue,登錄頁終于可以調(diào)用配置數(shù)據(jù)了

<template>
 <!--  底部  -->
    <div v-if="$store.state.setting.showFooter" id="el-login-footer">
      <span v-html="$store.state.setting.footerTxt"/>
      <span> ? </span>
      <a  target="_blank">{{$store.state.setting.caseNumber}}</a>
    </div>
  </div>
</template>
  1. 請求異步操作結(jié)束處理
    vuex中的dispatch和commit的使用:

dispatch 異步操作 this.$store.dispatch('actions的方法',arg)

commit 同步操作 this.$store.commit('mutations的方法',arg)

? Vuex結(jié)合 Promise 處理異步請求結(jié)束

  ```js

updateLoadMenus({ commit }) {
return new Promise((resolve, reject) => {
commit('SET_LOAD_MENUS', false)
})
}
```

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容