Vuex

Vuex簡(jiǎn)介:

由于使用propes傳遞數(shù)據(jù)太麻煩,所以使用vuex進(jìn)行狀態(tài)管理。
不能直接修改vuex中的數(shù)據(jù),只能調(diào)用方法修改數(shù)據(jù)數(shù)據(jù)。
vuex的核心:store(倉庫)
vuex是響應(yīng)式的

核心四個(gè)點(diǎn):

  • state

在項(xiàng)目中引入store
在computed中調(diào)用state數(shù)據(jù):

    conputed:{
        count(){
            return store.state.count
        }
    }

在methods中調(diào)用方法mutations方法:

    methods:{
        increment(){
            store.commit('increment')
        }
    }
mapState

當(dāng)一個(gè)組件需要多個(gè)狀態(tài)時(shí),幫助生產(chǎn)計(jì)算屬性得到state中的數(shù)據(jù)。
當(dāng)映射的計(jì)算屬性的名稱與state的子節(jié)點(diǎn)名稱相同時(shí),可以直接傳遞一個(gè)字符串?dāng)?shù)組。

    computed:mapState(['count','color']}

這樣一來,count就是state中的count,color就是state中的color

對(duì)象展開運(yùn)算符,當(dāng)computed中還有其他屬性時(shí),可以使用對(duì)象展開運(yùn)算符將mapState返回的對(duì)象合并到computed中。

  • getter

從store中的state中派生出一些狀態(tài),如doneTodosCount,
當(dāng)多個(gè)組件都需要用到這個(gè)方法(屬性)時(shí),可以將其放入store里的getters中,使其成為getters的一個(gè)計(jì)算屬性

    const store = new Vuex.Store({
        state: {
                todos: [
                    { id: 1, text: '...', done: true },
                    { id: 2, text: '...', done: false }
                ]
        },
        getters: {
                doneTodos: state => {
                    return state.todos.filter(todo => todo.done)
                }
        }
    })

此時(shí)可以使用store.getters.doneTodos 來獲取數(shù)據(jù)。
getters也可以接受其他getters作為參數(shù)。
mapGetters輔助函數(shù),和mapState類似,如果想要將getter屬性另取一個(gè)名字,使用對(duì)象形式:

import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
  // 使用對(duì)象展開運(yùn)算符將 getter 混入 computed 對(duì)象中
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}
//另取名字,使用對(duì)象形式
mapGetters({
  // 映射 `this.doneCount` 為 `store.getters.doneTodosCount`
  doneCount: 'doneTodosCount'
})
  • mutation

非常類似于事件,每一個(gè)都有字符串的事件類型(type)和一個(gè)回掉函數(shù)(handler),這個(gè)回掉函數(shù)就是實(shí)際進(jìn)行狀態(tài)更改的地方,并且會(huì)接受state作為第一個(gè)參數(shù)。
如state例子中的mutation,
在調(diào)用mutation時(shí),不可以直接調(diào)用handler(回掉函數(shù))只能通過commit方法傳入事件類型的字符串進(jìn)行mutation的調(diào)用。

const store = new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    increment (state) {
      // 變更狀態(tài)
      state.count++
    }
  }
})

store.commit('increment')

提交載荷(payload)
commit可以接受額外的參數(shù),多數(shù)情況下,載荷是個(gè)對(duì)象,其代碼風(fēng)格如下:

store.commit({
  type: 'increment',
  amount: 10
})

在大型項(xiàng)目中,通常將mutation作為常量寫入另一個(gè)文件中

// mutation-types.js
export const SOME_MUTATION = 'SOME_MUTATION'
// store.js
import Vuex from 'vuex'
import { SOME_MUTATION } from './mutation-types'

const store = new Vuex.Store({
  state: { ... },
  mutations: {
    // 我們可以使用 ES2015 風(fēng)格的計(jì)算屬性命名功能來使用一個(gè)常量作為函數(shù)名
    [SOME_MUTATION] (state) {
      // mutate state
    }
  }
})

mutation必須是同步函數(shù),沒有什么異步請(qǐng)求
組件中提交mutation,mapMutation

import { mapMutations } from 'vuex'

export default {
  // ...
  methods: {
    ...mapMutations([
      'increment', // 將 `this.increment()` 映射為 `this.$store.commit('increment')`

      // `mapMutations` 也支持載荷:
      'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.commit('incrementBy', amount)`
    ]),
    ...mapMutations({
      add: 'increment' // 將 `this.add()` 映射為 `this.$store.commit('increment')`
    })
  }
}

與mapGtter類似。

mutation類似于vue實(shí)例中的methods,但是調(diào)用方法不同。
getters類似于vue實(shí)例中的computed。

  • Action

類似于mutation,不同在于:

  • action提交的是mutation,而不是直接改變狀態(tài)
  • action可以包含任意異步操作

action函數(shù)接受一個(gè)與store實(shí)例具有相同方法和屬性的context對(duì)象,使用context.commit提交一個(gè)mutation
或者使用context.state \ context.getters來獲取state和getters。
context不是store實(shí)例本身。

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

-###Module
當(dāng)一個(gè)應(yīng)用十分復(fù)雜時(shí),將所有狀態(tài)放在一個(gè)單一狀態(tài)樹時(shí)會(huì)使得store變得十分臃腫,所以可以使用module進(jìn)行分割。


const moduleA = {
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: { ... },
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a // -> moduleA 的狀態(tài)
store.state.b // -> moduleB 的狀態(tài)

每個(gè)模塊的第一個(gè)參數(shù)是其自身的state。

?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 安裝 npm npm install vuex --save 在一個(gè)模塊化的打包系統(tǒng)中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 3,036評(píng)論 0 7
  • Vuex是什么? Vuex 是一個(gè)專為 Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲(chǔ)管理應(yīng)用的所有組件...
    蕭玄辭閱讀 3,231評(píng)論 0 6
  • vuex 場(chǎng)景重現(xiàn):一個(gè)用戶在注冊(cè)頁面注冊(cè)了手機(jī)號(hào)碼,跳轉(zhuǎn)到登錄頁面也想拿到這個(gè)手機(jī)號(hào)碼,你可以通過vue的組件化...
    sunny519111閱讀 8,160評(píng)論 4 111
  • Vuex 的學(xué)習(xí)記錄 資料參考網(wǎng)址Vuex中文官網(wǎng)Vuex項(xiàng)目結(jié)構(gòu)示例 -- 購物車Vuex 通俗版教程N(yùn)uxt....
    流云012閱讀 1,537評(píng)論 0 7
  • 本文為轉(zhuǎn)載,原文:Vue學(xué)習(xí)筆記進(jìn)階篇——vuex核心概念 前言 本文將繼續(xù)上一篇 vuex文章 ,來詳細(xì)解讀一下...
    ChainZhang閱讀 1,703評(píng)論 0 13

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