解決vuex刷新狀態(tài)初始化的方法實(shí)現(xiàn)
vuex五種基本對象
- state:存儲狀態(tài)(變量)
- getters:對數(shù)據(jù)獲取之前的再次編譯,可以理解為state的計(jì)算屬性。我們在組件中使用$sotre.getters.fun()
- mutations:修改狀態(tài),并且是同步的。在組件中使用$store.commit('',params)。這個和我們組件中的自定義事件類似。
- actions:異步操作。在組件中使用是$store.dispath('')
- modules:store的子模塊,為了開發(fā)大型項(xiàng)目,方便狀態(tà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è)置全局訪問的state對象 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í)時監(jiān)聽state值得變化(最新狀態(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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vuejs2 + Webpack框架里,模擬下載的實(shí)例講解
今天小編就為大家分享一篇Vuejs2 + Webpack框架里,模擬下載的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue 子組件與數(shù)據(jù)傳遞問題及注意事項(xiàng)
這篇文章主要介紹了Vue子組件與數(shù)據(jù)傳遞問題及需要注意事項(xiàng),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07uniapp項(xiàng)目國際化標(biāo)準(zhǔn)的配置與實(shí)現(xiàn)
UniApp是一種基于Vue.js的跨平臺開發(fā)框架,可以快速地開發(fā)同時運(yùn)行在多個平臺的應(yīng)用程序,這篇文章主要介紹了uniapp項(xiàng)目國際化標(biāo)準(zhǔn)的配置與實(shí)現(xiàn),需要的朋友可以參考下2023-11-11Vue2響應(yīng)式系統(tǒng)之set和delete
這篇文章主要介紹了Vue2響應(yīng)式系統(tǒng)之set和delete,通過為對象收集依賴,將對象、數(shù)組的修改、刪除也變成響應(yīng)式的了,同時為用戶提供了和方法,下文詳細(xì)介紹需要的朋友可以參考一下2022-04-04@vue/cli4升級@vue/cli5?node.js?polyfills錯誤的解決方式
最近在升級vue/cli的具有了一些問題,解決問題的過程也花費(fèi)了些時間,所以下面這篇文章主要給大家介紹了關(guān)于@vue/cli4升級@vue/cli5?node.js?polyfills錯誤的解決方式,需要的朋友可以參考下2022-09-09vue實(shí)現(xiàn)在一個方法執(zhí)行完后執(zhí)行另一個方法的示例
今天小編就為大家分享一篇vue實(shí)現(xiàn)在一個方法執(zhí)行完后執(zhí)行另一個方法的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08