vuex的基本用法

個(gè)人對(duì)vuex的理解是,state中定義的變量類似java中的全局變量,將各組件用到的變量集中起來(lái),統(tǒng)一管理,而getter,mutation,action就是針對(duì)這些全局變量的查詢和更新函數(shù)

  • state

作用:全局變量存放的地方,可以是一個(gè)字符串、數(shù)據(jù)、對(duì)象等

定義:

export default new Vuex.Store({
    state:{
        count:0,
        price:0,
        metaInfo:{
            title:"這是一個(gè)標(biāo)題",
             keywords:"vuex,vue.js,vue-router",
        }
    }
})
  • getter

作用:相當(dāng)于計(jì)算屬性

定義:

export default new Vuex.Store({
    state:{
        count:0,
        price:0,
        metaInfo:{
            title:"這是一個(gè)標(biāo)題",
             keywords:"vuex,vue.js,vue-router",
        }
    },
    getters:{
        getMoney: function(state){
            return state.count * state.price
        },
        getMoney2: function(state){
            return state.count * state.price
        },
    }
})

引用:

常規(guī)方式:

this.$store.getters.getMoney

mapGetters方式,有2種方式

? 1.使用對(duì)象的方法引用,這里可以同時(shí)引用多個(gè)

  • ...mapGetters({getMoneyFromState : 'getMoney',getMoneyFromState2 : 'getMoney2'}), 
        
    ....
    
    this.getMoneyFromState  //使用時(shí)這樣調(diào)用
    

    2.使用名稱的方法引用

  • ...mapGetters(['getMoney','getMoney2'])   //
    ...
    this.getMoney  //使用時(shí)這樣調(diào)用
    

1和2的效果是一樣的

說(shuō)白了,mapGetter只是提供了一種引用方式而已,避免每次引用寫那么長(zhǎng)的表達(dá)式

mapActions,mapMutations 都是這個(gè)思路

  • mutation

作用:用于定義改變state中變量的方法,畢竟直接調(diào)用的方式不是太優(yōu)雅

定義:

Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    count: 0,
    price: 5.5,
    metaInfo: {
      title: "這是一個(gè)標(biāo)題",
      keywords: "vuex,vue.js,vue-router"
    }
  },
  mutations: {
    updatePrice(state,price){
      state.price = price;
    },
    updateCount(state,count){
      state.count = count
    }
  }
})

每一個(gè)mutation方法帶兩個(gè)參數(shù),state和入?yún)?,mutation必須同步執(zhí)行

常規(guī)調(diào)用方法:

 this.$store.commit("updatePrice", val);

使用mapMutations方式:

export default {
  methods:{
      ...mapMutations([
        'updatePrice',
        'updateCount'
      ]),
     //使用時(shí)直接按方法調(diào)用即可
      inputPrice(price){
      this.updatePrice(price);
    }
  }
}

mutation是方法,所以這里要集成到methods里面

  • action

作用:將mutation里面處理數(shù)據(jù)的方法變成可異步執(zhí)行的方法,一個(gè)action中可調(diào)用多個(gè)mutation方法

定義:

export default new Vuex.Store({
  state: {
    count: 0,
    price: 5.5,
    metaInfo: {
      title: "這是一個(gè)標(biāo)題",
      keywords: "vuex,vue.js,vue-router"
    }
  },
  getters:{
    getMoney: function(state){
      return state.count * state.price
  },
  getMoney2: function(state){
    return state.count * state.price
}
  },
  mutations: {
    updatePrice(state,price){
      state.price = price;
    },
    updateCount(state,count){
      state.count = count
    },
  actions: {
    updateOrderInfo(context,info)
    {
      context.commit('updatePrice',info.price);
      context.commit('updateCount',info.count);
    }
  },
  modules: {}
});

引用:

import {mapActions } from 'vuex'
methods:{
      ...mapActions(['updateOrderInfo']),
      inputPrice(){
      this.updatePrice(this.price);
      this.$store.state.price ;
    },
    getMyMoney(){
      this.updateOrderInfo({price:this.price,count:this.count});
    },
    getMyMoney2(){
      this.$store.dispatch('updateOrderInfo',{price:this.price,count:this.count});
    }
  }


常規(guī)方式,利用dispatch

this.$store.dispatch('updateOrderInfo',{price:this.price,count:this.count});

mapAction方式:

this.updateOrderInfo({price:this.price,count:this.count});

  • 總結(jié)

state:用于定義全局變量

getter:將state封裝成計(jì)算屬性,便于獲取全局變量

mutation:定義改變state中全局變量的方式,這些方法都是同步的

action:定義改變state的異步方法

state,getter,mutation,action 都可以通過(guò)常規(guī)的this.$store來(lái)直接引用

mapGetters,mapActions,mapMutations只是定義了另外一種引用方式,避免每次寫長(zhǎng)的表達(dá)式,

用哪種方式,看個(gè)人喜好!

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

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

  • 由于Vuex的官方文檔在各個(gè)模塊之間缺乏一些過(guò)渡,另外新概念很多,使得初讀時(shí)總有些云里霧里的感覺(jué)。于是本文在官方文...
    一郭鮮閱讀 393評(píng)論 0 1
  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類 // 創(chuàng)建一...
    蕓豆_6a86閱讀 789評(píng)論 0 3
  • State 單一狀態(tài)樹(shù) Vuex使用單一狀態(tài)樹(shù)——用一個(gè)對(duì)象就包含了全部的應(yīng)用層級(jí)狀態(tài)。至此它便作為一個(gè)“唯一數(shù)據(jù)...
    oWSQo閱讀 1,174評(píng)論 0 0
  • 安裝 npm npm install vuex --save 在一個(gè)模塊化的打包系統(tǒng)中,您必須顯式地通過(guò)Vue.u...
    蕭玄辭閱讀 3,049評(píng)論 0 7
  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類 // 創(chuàng)建一...
    蕓豆_6a86閱讀 444評(píng)論 0 0

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