- state:vuex的基本数据,用来存储变量
- geeter:(可以认为是store的计算属性)。就像计算属性一样,getter的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。
- mutation:提交更新数据的方法,必须是同步的(如果需要异步使用action)。
- action:和mutation的功能大致相同,不同之处在于 Action 提交的是 mutation,而不是直接变更状态。 Action 可以包含任意异步或多个同步操作
- modules:模块化vuex,可以让每一个模块拥有自己的state、mutation、action、getters,使得结构非常清晰,方便管理。
目录结构
- store
- actions
- mutations
- getters
- state
- modules
- index.js
代码展示
export default {
changeLoginFn ({commit},value) {
console.log(value)
setTimeout(() => {
commit('setLoginFn',value.status)
}, 2000)
}
}
export default {
setNameFn(state, value) {
state.name = value
},
setLoginFn(state, value){
state.isLogin = value
console.log(state.isLogin)
}
}
export default {
getTodoById (state) {
return (id) => {
return state.name + state.name2 + id
}
},
setSpliceFn (state, getters,) {
return state.name + state.name2
}
}
export default {
name: '小鸡',
name2: '啄米',
isLogin: 1
}
export default {
a: {
namespaced: true,
state: {
num: 1
}
},
b: {
state: {
num: 1
}
}
....
}
import Vue from 'vue'
import Vuex from 'vuex'
import state from "./state/state";
import mutations from "./mutations/mutations";
import actions from "./actions/actions";
import getters from "./getters/getters";
import modules from "./modules/modules";
Vue.use(Vuex)
var storePublics = {
state,
mutations,
actions,
getters,
modules,
};
if (sessionStorage.getItem("store")) {
storePublics.state = JSON.parse(sessionStorage.getItem("store"))
sessionStorage.removeItem("store")
}
window.addEventListener("beforeunload", () => {
sessionStorage.setItem("store",JSON.stringify(storePublics.state))
});
let store = new Vuex.Store(storePublics)
store.watch((state)=>{
state.name + state.name1
console.log('当state值发生改变就会触发回调函数')
},()=>{
console.log('回调函数')
})
store.subscribe((mutation,state)=>{
console.log(mutation)
console.log(mutation.payload)
console.log('每次调用mutations 都会执行这个函数')
})
store.subscribeAction((action, state) => {
console.log(action)
console.log(action.payload)
console.log('监听action')
})
export function createStore() {
return store
}
import Vue from 'vue'
import App from './App.vue'
import { createRouter } from './router'
import { createStore } from './store'
import { publiceCom } from "./publiceComponent";
Vue.use(publiceCom);
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
Vue.config.productionTip = false;
export function createApp () {
const router = createRouter();
const store = createStore();
const app = new Vue({
router,
store,
render: h => h(App)
});
return { app ,router,store}
}
使用方法
<template>
<div class="home">
<p >她的名称: {{name}} </p>
<p>拼接的名称:{{setSpliceFn}}</p>
<p>拼接的名称:{{getTodoById('haha')}}</p>
<p>登录状态:{{isLogin}}</p>
<el-button type="primary" @click="nameClickFn">改变名称</el-button>
<el-button type="primary" @click="loginClickFn">请求后台切换登录状态</el-button>
</div>
</template>
<script>
import {mapState,mapMutations,mapActions,mapGetters} from 'vuex'
export default {
name: 'Home',
data () {
return {
flagName: true,
}
},
computed: {
...mapState(['name','isLogin']),
...mapGetters([
'setSpliceFn',
'getTodoById'
]),
},
methods: {
...mapMutations([
'setNameFn',
'setLoginFn'
]),
...mapActions([
'changeLoginFn'
]),
nameClickFn() {
this.flagName = !this.flagName
if(this.flagName){
this.setNameFn('小鸡 ');
}else {
this.setNameFn('小鸭')
}
},
loginClickFn () {
let value = {
that: this,
status: 2
}
this.changeLoginFn(value)
},
},
mounted() {
console.log(this.$store.state)
}
}
</script>
<style scoped>
#nav {
padding: 30px;
}
#nav span {
font-weight: bold;
color: #2c3e50;
cursor: pointer;
}
#nav span .active {
color: #42b983;
}
</style>