解決vuex刷新?tīng)顟B(tài)初始化的方法實(shí)現(xiàn)
vuex五種基本對(duì)象
- state:存儲(chǔ)狀態(tài)(變量)
- getters:對(duì)數(shù)據(jù)獲取之前的再次編譯,可以理解為state的計(jì)算屬性。我們?cè)诮M件中使用$sotre.getters.fun()
- mutations:修改狀態(tài),并且是同步的。在組件中使用$store.commit('',params)。這個(gè)和我們組件中的自定義事件類似。
- actions:異步操作。在組件中使用是$store.dispath('')
- modules:store的子模塊,為了開發(fā)大型項(xiàng)目,方便狀態(tài)管理而使用的。這里我們就不解釋了,用起來(lái)和上面的一樣。
npm install vuex -S // 安裝vuex
src/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import temp from '@/store/modules/temp'
Vue.use( Vuex ); // 掛載在vue
const store = new Vuex.Store({
modules: {
temp,
}, state: {
}, getters: {
}, mutations: {
},
});
export default store; // 拋出
src/store/modules/temp.js
const Storage = sessionStorage
const tempInfo = {
state: { // 設(shè)置全局訪問(wèn)的state對(duì)象
tempData: Storage['SET_TEMP_DATA'] ? JSON.parse(Storage['SET_TEMP_DATA']) : {}, // 設(shè)置初始化的值(Storage中是否存在,存在則獲取,不存在則默認(rèn)賦值{})
}, mutations: { // 自定義改變state初始值的方法,這里面的參數(shù)除了state之外還可以再傳額外的參數(shù)(變量或?qū)ο?;
SET_TEMP_DATA(state, tempData) {
state.tempData = tempData
},
}, actions: {
SetData({ commit }, tempData) {
commit('SET_TEMP_DATA', tempData); // 同步操作
Storage.setItem('SET_TEMP_DATA', JSON.stringify(tempData))
}
}, getters: { // 實(shí)時(shí)監(jiān)聽(tīng)state值得變化(最新?tīng)顟B(tài))
tempData: (state) => {
return state.tempData
}
}
}
export default tempInfo;
main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import store from '@/store/index' //vuex 狀態(tài)管理
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store, // 使用store
components: { App },
template: '<App/>'
})
src/index.vue
<template>
<div class="move-forward">
<div @click="click">點(diǎn)擊改變vuex值</div>
</template>
<script>
export default {
methods: {
click() {
let aa = this.$store.getters.tempData.aaa*1
this.$store.dispatch('SetData', {"aaa": aa += 1})
},
}
}
</script>
其他
當(dāng)然還可以使用vuex-persistedstate、vuex-along等這些第三方插件。
npm i -S vuex-persistedstate或npm i -S vuex-along
import Vue from 'vue'
import Vuex from 'vuex'
import temp from '@/store/modules/temp'
import createPersistedSatte from 'vuex-persistedstate' // 引入
Vue.use( Vuex );
const store = new Vuex.Store({
modules: {
temp,
}, state: {
}, getters: {
}, mutations: {
},
plugins: [createPersistedSatte()], // 掛載插件
});
export default store
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件詳解
這篇文章主要介紹了vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
element-ui?table使用type='selection'復(fù)選框全禁用(全選禁用)詳解
element-ui中的table的多選很好用,但是如果其中某一項(xiàng)禁止選擇,該怎樣操作呢,下面這篇文章主要給大家介紹了關(guān)于element-ui?table使用type='selection'復(fù)選框全禁用(全選禁用)的相關(guān)資料,需要的朋友可以參考下2023-01-01
vue中的input框點(diǎn)擊后不聚焦問(wèn)題
這篇文章主要介紹了vue中的input框點(diǎn)擊后不聚焦問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue 動(dòng)態(tài)組件(component :is) 和 dom元素限制(is)用法說(shuō)明
這篇文章主要介紹了vue 動(dòng)態(tài)組件(component :is) 和 dom元素限制(is)用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue cli 3.0通用打包配置代碼,不分一二級(jí)目錄
這篇文章主要介紹了vue cli 3.0通用打包配置代碼,不分一二級(jí)目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue實(shí)現(xiàn)頁(yè)面加載動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁(yè)面加載動(dòng)畫效果,vue頁(yè)面出現(xiàn)正在加載的初始頁(yè)面與實(shí)現(xiàn)動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Vue父組件調(diào)用子組件函數(shù)實(shí)現(xiàn)
這篇文章主要介紹了Vue父組件調(diào)用子組件函數(shù)實(shí)現(xiàn),全文通過(guò)舉例子及代碼的形式進(jìn)行了一個(gè)簡(jiǎn)單的介紹,希望大家能夠理解并且學(xué)習(xí)到其中知識(shí)2021-08-08

