明霞山资源网 Design By www.htccd.com
vuex五种基本对象
- state:存储状态(变量)
- getters:对数据获取之前的再次编译,可以理解为state的计算属性。我们在组件中使用$sotre.getters.fun()
- mutations:修改状态,并且是同步的。在组件中使用$store.commit('',params)。这个和我们组件中的自定义事件类似。
- actions:异步操作。在组件中使用是$store.dispath('')
- modules:store的子模块,为了开发大型项目,方便状态管理而使用的。这里我们就不解释了,用起来和上面的一样。
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: { // 设置全局访问的state对象
tempData: Storage['SET_TEMP_DATA'] "htmlcode">
import Vue from 'vue'
import App from './App'
import router from './router'
import store from '@/store/index' //vuex 状态管理
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">点击改变vuex值</div>
</template>
<script>
export default {
methods: {
click() {
let aa = this.$store.getters.tempData.aaa*1
this.$store.dispatch('SetData', {"aaa": aa += 1})
},
}
}
</script>
其他
当然还可以使用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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...