• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

    Vue的Flux框架之Vuex狀態(tài)管理器

    來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:33:44
    文檔

    Vue的Flux框架之Vuex狀態(tài)管理器

    Vue的Flux框架之Vuex狀態(tài)管理器:學(xué)習(xí)vue之前,最重要是弄懂兩個(gè)概念,一是what,要理解vuex是什么;二是why,要清楚為什么要用vuex。 Vuex是什么? Vuex 類似 React 里面的 Redux 的狀態(tài)管理器,用來管理Vue的所有組件狀態(tài)。 為什么使用Vuex? 當(dāng)你打算開發(fā)大型單頁應(yīng)用(SP
    推薦度:
    導(dǎo)讀Vue的Flux框架之Vuex狀態(tài)管理器:學(xué)習(xí)vue之前,最重要是弄懂兩個(gè)概念,一是what,要理解vuex是什么;二是why,要清楚為什么要用vuex。 Vuex是什么? Vuex 類似 React 里面的 Redux 的狀態(tài)管理器,用來管理Vue的所有組件狀態(tài)。 為什么使用Vuex? 當(dāng)你打算開發(fā)大型單頁應(yīng)用(SP

    學(xué)習(xí)vue之前,最重要是弄懂兩個(gè)概念,一是“what”,要理解vuex是什么;二是“why”,要清楚為什么要用vuex。

    Vuex是什么?

    Vuex 類似 React 里面的 Redux 的狀態(tài)管理器,用來管理Vue的所有組件狀態(tài)。

    為什么使用Vuex?

    當(dāng)你打算開發(fā)大型單頁應(yīng)用(SPA),會出現(xiàn)多個(gè)視圖組件依賴同一個(gè)狀態(tài),來自不同視圖的行為需要變更同一個(gè)狀態(tài)。
    遇到以上情況時(shí)候,你就應(yīng)該考慮使用Vuex了,它能把組件的共享狀態(tài)抽取出來,當(dāng)做一個(gè)全局單例模式進(jìn)行管理。這樣不管你在何處改變狀態(tài),都會通知使用該狀態(tài)的組件做出相應(yīng)修改。

    下面講解如何使用Vuex

    一個(gè)簡單的Vuex示例

    本文就講解安裝Vuex,直接通過代碼講解Vuex使用。

    import Vue from 'vue';
    import Vuex from 'vuex';
    Vue.use(Vuex);
    
    const store = new Vuex.Store({
     state: {
     count: 0
     },
     mutations: {
     increment (state) {
     state.count++
     }
     }
    })
    
    

    上面就是一個(gè)簡單的Vuex示例,每一個(gè)Vuex應(yīng)用就是一個(gè)store,在store中包含組件中的共享狀態(tài)state和改變狀態(tài)的方法(暫且稱作方法)mutations。

    需要注意的是只能通過mutations改變store的state的狀態(tài),不能通過store.state.count = 5;直接更改(其實(shí)可以更改,不建議這么做,不通過mutations改變state,狀態(tài)不會被同步)。

    使用store.commit方法觸發(fā)mutations改變state:

    store.commit('increment');
    
    console.log(store.state.count) // 1
    

    一個(gè)簡簡單單的Vuex應(yīng)用就實(shí)現(xiàn)了。

    在Vue組件使用Vuex

    如果希望Vuex狀態(tài)更新的時(shí)候,組件數(shù)據(jù)得到相應(yīng)的更新,那么可以用計(jì)算屬性computed獲取state的更新狀態(tài)。

    const Counter = {
     template: `<div>{{ count }}</div>`,
     computed: {
     count () {
     return store.state.count;
     }
     }
    }
    

    每一個(gè)store.state都是全局狀態(tài),在使用Vuex時(shí)候需要在根組件或(入口文件)注入。

    // 根組件
    import Vue from 'vue';
    import Vuex from 'vuex';
    
    Vue.use(Vuex);
    const app = new Vue({
     el: '#app',
     store,
     components: {
     Counter
     },
     template: `
     <div class="app">
     <counter></counter>
     </div>
     `
    })
    
    

    通過這種注入機(jī)制,就能在子組件Counter通過this.$store訪問:

    // Counter 組件
    const Counter = {
     template: `<div>{{ count }}</div>`,
     computed: {
     count () {
     return this.$store.state.count
     }
     }
    }
    
    

    mapState函數(shù)

    computed: {
     count () {
     return this.$store.state.count
     }
    }
    

    上面通過count計(jì)算屬性獲取同名state.count屬性,如何每一次獲取都要寫一個(gè)這樣的方法,是不顯得重復(fù)又麻煩?可以使用mapState函數(shù)簡化這個(gè)過程。

    import { mapState } from 'vuex';
    
    export default {
     computed: mapState ({
     count: state => state.count,
     countAlias: 'count', // 別名 `count` 等價(jià)于 state => state.count
     })
    }
    

    還有更簡單的使用方法:

    computed: mapState([
     'count' 
     // 映射 this.count 為 store.state.count
    ])

    Getters對象

    如果我們需要對state對象進(jìn)行做處理計(jì)算,如下:

    computed: {
     doneTodosCount () {
     return this.$store.state.todos.filter(todo => todo.done).length
     }
    }
    

    如果多個(gè)組件都要進(jìn)行這樣的處理,那么就要在多個(gè)組件中復(fù)制該函數(shù)。這樣是很沒有效率的事情,當(dāng)這個(gè)處理過程更改了,還有在多個(gè)組件中進(jìn)行同樣的更改,這就更加不易于維護(hù)。

    Vuex中g(shù)etters對象,可以方便我們在store中做集中的處理。Getters接受state作為第一個(gè)參數(shù):

    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)
     }
     }
    })

    在Vue中通過store.getters對象調(diào)用:

    computed: {
     doneTodos () {
     return this.$store.getters.doneTodos
     }
    }

    Getter也可以接受其他getters作為第二個(gè)參數(shù):

    getters: {
     doneTodos: state => {
     return state.todos.filter(todo => todo.done)
     },
     doneTodosCount: (state, getters) => {
     return getters.doneTodos.length
     }
    }

    mapGetters輔助函數(shù)

    與mapState類似,都能達(dá)到簡化代碼的效果。mapGetters輔助函數(shù)僅僅是將store中的getters映射到局部計(jì)算屬性:

    import { mapGetters } from 'vuex'
    
    export default {
     // ...
     computed: {
     // 使用對象展開運(yùn)算符將 getters 混入 computed 對象中
     ...mapGetters([
     'doneTodosCount',
     'anotherGetter',
     // ...
     ])
     }
    }
    
    

    上面也可以寫作:

    computed: mapGetters([
     'doneTodosCount',
     'anotherGetter',
     // ...
     ])
    

    所以在Vue的computed計(jì)算屬性中會存在兩種輔助函數(shù):

    import { mapState, mapGetters } from 'vuex';
    
    export default {
     // ...
     computed: {
     ...mapGetters([ ... ]),
     ...mapState([ ... ])
     }
    }
    
    

    Mutations

    之前也說過了,更改Vuex的store中的狀態(tài)的唯一方法就是mutations。

    每一個(gè)mutation都有一個(gè)事件類型type和一個(gè)回調(diào)函數(shù)handler。

    調(diào)用mutation,需要通過store.commit方法調(diào)用mutation type:

    store.commit('increment')

    Payload 提交載荷

    也可以向store.commit傳入第二參數(shù),也就是mutation的payload:

    mutaion: {
     increment (state, n) {
     state.count += n;
     }
    }
    
    store.commit('increment', 10);
    
    

    單單傳入一個(gè)n,可能并不能滿足我們的業(yè)務(wù)需要,這時(shí)候我們可以選擇傳入一個(gè)payload對象:

    mutation: {
     increment (state, payload) {
     state.totalPrice += payload.price + payload.count;
     }
    }
    
    store.commit({
     type: 'increment',
     price: 10,
     count: 8
    })
    
    

    mapMutations函數(shù)

    不例外,mutations也有映射函數(shù)mapMutations,幫助我們簡化代碼,使用mapMutations輔助函數(shù)將組件中的methods映射為store.commit調(diào)用。

    import { mapMutations } from 'vuex'
    
    export default {
     // ...
     methods: {
     ...mapMutations([
     'increment' // 映射 this.increment() 為 this.$store.commit('increment')
     ]),
     ...mapMutations({
     add: 'increment' // 映射 this.add() 為 this.$store.commit('increment')
     })
     }
    }
    
    

    Actions

    注 Mutations必須是同步函數(shù)。

    如果我們需要異步操作和提交多個(gè)Mutations,Mutations就不能滿足我們需求了,這時(shí)候我們就需要Actions了。

    Actions

    Action 類似于 mutation,不同在于:

    1. Action 提交的是 mutation,而不是直接變更狀態(tài)。
    2. Action 可以包含任意異步操作。

    讓我們來注冊一個(gè)簡單的 action:

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

    分發(fā) Action

    Action 函數(shù)接受一個(gè)與 store 實(shí)例具有相同方法和屬性的 context 對象,因此你可以調(diào)用 context.commit 提交一個(gè) mutation,或者通過 context.state 和 context.getters 來獲取 state 和 getters。

    分發(fā) Action

    Action 通過 store.dispatch 方法觸發(fā):

    乍一眼看上去感覺多此一舉,我們直接分發(fā) mutation 豈不更方便?實(shí)際上并非如此,還記得 mutation必須同步執(zhí)行這個(gè)限制么?Action就不受約束! 我們可以在 action 內(nèi)部執(zhí)行異步操作:

    actions: {
     incrementAsync ({ commit }) {
     setTimeout(() => {
     commit('increment')
     }, 1000)
     }
    }

    Actions 支持同樣的載荷方式和對象方式進(jìn)行分發(fā):

    // 以載荷形式分發(fā)
    store.dispatch('incrementAsync', {
     amount: 10
    })
    
    // 以對象形式分發(fā)
    store.dispatch({
     type: 'incrementAsync',
     amount: 10
    })
    

    mapActions

    同樣地,action也有相對應(yīng)的mapActions 輔助函數(shù)

    mapActions

    mapActions 輔助函數(shù)跟mapMutations一樣都是組件的 methods 調(diào)用:

    import { mapActions } from 'vuex'
    
    export default {
     // ...
     methods: {
     ...mapActions([
     'increment' // 映射 this.increment() 為 this.$store.dispatch('increment')
     ]),
     ...mapActions({
     add: 'increment' // 映射 this.add() 為 this.$store.dispatch('increment')
     })
     }
    }
    

    mutation-types

    關(guān)于mutation-types方面的講解官方文檔很少說明,但在實(shí)際的中大項(xiàng)目中,對==mutation-types==的配置是必不可少的,Vuex的文檔只講解了state,getters,mutation,actions四個(gè)核心概念,下面我簡單補(bǔ)充下mutation-types的使用。

    顧名思義,==mutation-types==其實(shí)就是mutation實(shí)例中各個(gè)方法的設(shè)定,一般要mutation方法前先在mutation-types用大寫寫法設(shè)定,再在mutation里引入使用,下面看看項(xiàng)目實(shí)際使用:

    項(xiàng)目組織結(jié)構(gòu)


    在mutation-types定義好mutation的方法結(jié)構(gòu):

    //SET_SINGER,SET_SONG 為mutation中要使用的方法名
    
    export const SET_SINGER = 'SET_SINGER'
    
    export const SET_SONG = 'SET_SONG'
    
    

    在mutation中導(dǎo)入使用:

    import * as types from ',/mutation-types.js'
    
    const mutations = {
     [types.SET_SINGER](state, singer) {
     .... 
     },
     [types.SET_SONG](state, song) {
     .... 
     }
    }

    結(jié)語

    看完上面對vuex的講解相信你已經(jīng)入門了,現(xiàn)在可以看看具體的項(xiàng)目加深理解,可以參考我的github一個(gè)購物車?yán)? https://github.com/osjj/vue-shopCart

    聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    Vue的Flux框架之Vuex狀態(tài)管理器

    Vue的Flux框架之Vuex狀態(tài)管理器:學(xué)習(xí)vue之前,最重要是弄懂兩個(gè)概念,一是what,要理解vuex是什么;二是why,要清楚為什么要用vuex。 Vuex是什么? Vuex 類似 React 里面的 Redux 的狀態(tài)管理器,用來管理Vue的所有組件狀態(tài)。 為什么使用Vuex? 當(dāng)你打算開發(fā)大型單頁應(yīng)用(SP
    推薦度:
    標(biāo)簽: VUE 管理 vu
    • 熱門焦點(diǎn)

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 亚洲日韩国产AV无码无码精品| 亚洲动漫精品无码av天堂| 欧美成人精品一区二三区在线观看 | 精品无人码麻豆乱码1区2区| 国产成人精品日本亚洲专区| 99久久精品国产麻豆| 亚洲av永久无码精品国产精品| 久久99精品久久久久久野外| 办公室久久精品| 久久九九青青国产精品| 国产精品9999久久久久| 四虎成人精品无码| 在线精品亚洲| 国产精品视频二区不卡| 亚洲国产精品乱码一区二区| 久久久久亚洲精品天堂久久久久久| 97视频在线精品国自产拍| 亚洲av无码成人精品国产| 久久精品国产欧美日韩| 国产精品手机在线观看你懂的| 精品亚洲一区二区| 国产成人精品天堂| 国产精品免费大片| 国产办公室秘书无码精品99| 国99精品无码一区二区三区| 国产精品午睡沙发系列| 久久精品人人做人人爽97| 久久亚洲中文字幕精品有坂深雪| 亚洲AV无码之日韩精品| 亚洲国产成人乱码精品女人久久久不卡| 久久久无码精品亚洲日韩软件| 久久精品国产精品亚洲下载| 日本五区在线不卡精品| 亚洲精品国产福利一二区| 亚洲AV永久青草无码精品| 久久精品国产第一区二区三区| 国产精品无码无需播放器| 国产精品高清一区二区人妖| 日本欧美韩国日本精品| 国产精品99| 日韩精品成人亚洲专区|