1.什么是vuex?
Vuex是一个专门为vue.js应用程序发开的状态管理模式。它采用集中式存储和管理程序的所有组件的数据
2.vuex的五大核心组成
state
:所有的数据都存在state中,state是一个对象
mutations
:它可以直接操作state中的数据
actions
:只能调用mutations中的方法
getters
:类似组件中的计算属性,实现对state中的数据做一些逻辑性的操作
modules
:将仓库分模块化存储
3.如何使用呢?看下方
首先说一下vuex的运行机制:
在组件中通过dispath来调用actions中的方法在actions中通过commit来调用mutations中的方法,在mutations中可以直接操作state中的数据,state的数据只要一发生改变立马响应到组件中。
store——>index.js
import vue from 'vue'
import Vuex from 'vuex'
Vue.use(vuex);
const state= ()=>{ token:''}
const actions = {
set_token({commit},val){
commit("to_token",val)
}
}
const mutations = {
to_token(state,val){
state.token=val;
}
}
const getters = {}
let store = new Vuex.store({
state,
actions,
mutations,
getters
})
module.export=store;
home.vue
<template>
<div>
{{$store.state.token}}
</div>
</template>
<script>
export default={
name: 'Home',
data() {
return {
tel: '',
}
},
created(){
//调用acionts中的方法
this.$store.dispatch('set_token',12345);
//调用mutations中的方法
this.$store.commit('to_token',123456)
}
}
<script>
4.高级用法-之数据持久化
问题:存储在vuex中的数据,刷新页面,会丢失。
为了解决刷新页面丢失数据问题,才有了持久化。
最简单的方法就是利用插件vuex-persistedstate
。
1.安装
cnpm install vuex-persistedstate -S
备注:
-S是
--save
的简写,意为:把插件安装到dependencies
(生产环境依赖)中
-D是--save-dev
的简写,意为:把插件安装到devpendedcies
(开发环境依赖)中
2.使用
import createPersistedState from 'vuex-persistedstate'
const store = new Vuex.Store({
state,
mutations,
actions,
getters,
plugins: [createPersistedState({
storage: sessionStorage,
key: "token"
})]//会自动保存创建的状态。刷新还在
})
参数:
storage:存储方式。(sessionStorage,loaclStarage) key:定义本地存储中的key
5.高级语法-值 模块化管理数据 (modules)
1.什么时候需要用到模块
管理vuex数据
项目庞大,数据信息量特别大的时候,我们可以考虑分模块形式管理数据,比如user模块管理用户信息数据库,cart模块管理购物车数据,shop模块管理商品信息数据。
import vue from 'vue'
import Vuex from 'vuex'
Vue.use(vuex);
const state= ()=>{ token:''}
const actions = {
set_token({commit},val){
commit("to_token",val)
}
}
const mutations = {
to_token(state,val){
state.token=val;
}
}
const getters = {}
//user模块
let user = {
namespaced: true, //一定要开始命名空间。
state: { userid: 1234 },
actions: {
},
mutations: {
SET_USERID(state, val) {
state.userid = val;
}
},
getters: {
}
}
//购物车数据的模块
let cart = {
namespaced: true,
state: { userid: 567 },
actions: {
},
mutations: {
},
getters: {
}
}
const store = new Vuex.Store({
state,
mutations,
actions,
getters,
modules: {
user,
cart
},
plugins: [createPersistedState({
storage: sessionStorage,
key: "token"
})]//会自动保存创建的状态。刷新还在
})
export default store
home.vue如何使用
获取user模块的`userid`
this.$store.state.user.userid
this.$store.commit("SET_USERID",12345)
6.高级用法-之 辅助函数(语法糖)
1.有哪几个辅助函数?
mapState,mapActions,mapMutations,mapGetter
2.辅助函数可以把vux
中的数据和方法映射到vue组件
中。达到简化操作的目的
3.如何使用
home.vue
<template>
<div id="">
{{ token }}
{{ token - x }}
</div>
</template>
<script>
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex'
import {createNamespacedHelpers} from 'vuex'
const {mapState:mapStateUser,mapActions:mapActionUser,mapMutations:mapMutaionuser} = createNamespacedHelpers('user')
const {mapState:mapStateCart,mapActions:mapActionCart,mapMutations:mapMutaionCart} = createNamespacedHelpers('cart')
export default {
name: '',
data() {
return {}
},
computed: {
...mapState({
token: 'token'
}),
...mapGetters(['token-x']),
...mapSateUser(['userid']),
...mapStateCart({cartid:'userid'})
},
//生命周期 - 创建完成(访问当前this实例)
created() {
this.setToken('123456')
},
//生命周期 - 挂载完成(访问DOM元素)
mounted() {},
methods: {
...mapActions({
setToken: 'setToken'
}),
...mapMutations(['SET_TOKEN']),
...mapMutaionUser({
setId:"setToken"
})
}
}
</script>