vue store狀態存儲管理


1、引入vuex,使用store存儲,一般存儲於內存中,刷新頁面后會丟失。

  用法:

import Vuex from 'vuex'
import config from '@/env/config';

Vue.use(Vuex)

export default new Vuex.Store({

    state: {
        deptName: '',
        title: '',
        word: ''
    },
    mutations: {
        //這里是set方法
        setDeptName(state, name) {
            state.deptName = name;
        }
    },
    actions: {
        setDeptName(context, name) {
            context.commit('setDeptName', name);
        }
    }
})
mutations 用於修改state內容的值,actions負責提交mutations
原則上 頁面通過 this.$store.dispatch("")來觸發對應的action,action里commit觸發mutations

2、引入vuex-along,實現存儲刷新不丟失,可以指定store內容存儲到localstorage或者sessionstorage中。 用法在 new store中添加plugins:[vuexAlong],(該方式默認將store的state的所有內容存儲到localstorage或sessionstorage中), 如果local和session 不配置,默認vuex-along存儲在localstorage中。

  用法:

plugins: [VueXAlong({
    name: 'along',     //存放在localStroage或者sessionStroage 中的名字
    local:{ list: [], isFilter: false}, //isFilter 配置false,代表list里的對象存儲在localstorage中
session: { list: [], isFilter: true } //isFilter設置為true時, list 數組中的值就會被過濾調,這些值不會存放在seesionstorage中
})]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM