vue項目回顧3【數(shù)據(jù)管理vuex】

結(jié)構(gòu):

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import store_recommend from './recommend'

Vue.use(Vuex);

const store = new Vuex.Store({
  state:{
    width_s:document.documentElement.clientWidth,
    count:1,
    slider:{"isopen":false},
    whichsonglist:false,
    headNav:'head-nav1',
    loading:false,
    ellipsisState:{"open":false,"data":null},
    },
  getters:{
    headNav:state=>state.headNav,
    count:state=>state.count,
    loading:state=>state.loading,
    ellipsisState:state=>state.ellipsisState
  },
  mutations:{
    lodingShow:(state)=>{
      state.loading = true;
    },
    ellipsisFn:(state,item_)=>{
      state.ellipsisState.open = !state.ellipsisState.open;
      state.ellipsisState.data = item_;
    },
    tool_audio:(state,el)=>{
      state.play.play = el;
    },
    currentTime_Fn:(state,time)=>{
      state.play.currentTime_ = time;
    },
    setInterval:(state)=>{
      state.play.currentTime_+=1;
    }
  },
  actions:{
    actionsFn:({commit,state})=>{
      commit('aa');
      console.log('ppppp');
    }
  },
  modules:{
    sr:store_recommend
  }
});

export default store;

五部分:
state :注冊數(shù)據(jù)管理參數(shù)
getters :用于獲取參數(shù)
mutations :改變參數(shù)值的方法容器
actions :用來操作多個mutations 方法
modules :分模塊管理數(shù)據(jù)

應(yīng)用方法如下:
1.數(shù)據(jù)獲取
store/index.js:

  state:{
    count:1
    }
  getters:{
    count:state=>state.count
  },

頁面:

 <li> <i class="fa fa-address-book"></i>最近播放<span>{{count}}</span></li>

------------------------------------------------------
  import { mapGetters,mapState,mapActions} from 'vuex'
 export default {
      加入:
      computed:{
        ...mapGetters(['count'])
      },

或者:
只注冊state然后直接:

thissong:this.$store.state.thissong.data

2.數(shù)據(jù)設(shè)置(mutations 相關(guān))
不用引用,直接

 this.$store.commit('tool_audio',false);

3.actions 相關(guān)

 <li @click="testfn()"> 最近播放<span>23</span></li>

  methods:{
           ...mapActions({testfn:'actionsFn'})
}

相當(dāng)于:

  methods:{
          testfn(){
              this.$store.dispatch('actionsFn')
          }
}

4.模塊
引入模塊是為了降低代碼中的耦合性,也方便管理
在上文中我們引入了一個store_recommend的模塊
state/index.js:

  modules:{
    sr:store_recommend
  }

state/store_recommend.js:

const store_recommend = {
  namespaced: true,
  state:{
    count:1
  },
  getters:{

  },
  mutations:{
    countFn:(state,count)=>{
      state.count+=count;
      }
  },
  actions:{

  }
}
export default store_recommend

應(yīng)用頁面:

 methods:{
        addcount(count_){
            this.$store.commit('sr/countFn',count_)
        }

好啦,該系列完畢嘿嘿

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

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

  • 一、初識VueX 1.1 關(guān)于VueX VueX是適用于在Vue項目開發(fā)時使用的狀態(tài)管理工具。試想一下,如果在一個...
    怪獸難吃素閱讀 325,721評論 24 314
  • 1.什么是狀態(tài): 可以理解為在data中的屬性需要分享給其他vue組件使用的部分,就叫做狀態(tài)。簡單的說就是data...
    JLong閱讀 1,206評論 0 0
  • 一、什么是Vuex Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有...
    紫月凌楓閱讀 10,259評論 0 16
  • 安裝 npm npm install vuex --save 在一個模塊化的打包系統(tǒng)中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 3,035評論 0 7
  • Vue現(xiàn)代化使用方法(四)--Vuex 在組件內(nèi)可以通過data屬性共享數(shù)據(jù),父子組件也可以通過props進(jìn)行數(shù)據(jù)...
    去年的牛肉閱讀 225評論 0 2

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