本文作者:端,映客的一位前端开发。
export const SET_LOGREG_SHOW = 'SET_LOGREG_SHOW'
import * as types from '../mutations-types'(不理解import as的自行看es6模块章节)
const state ={
show: false //1:登录 2:注册 3:找回密码
}
const getters = {
getLogregShow: state=> state.show
}
const mutations = {
[types.SET_ISLOGREGSHOW] (state,show){
state.show = show
}
}
const actions = {
setLogregShow({commit},show){
commit(types.SET_ISLOGREGSHOW,show)
}
}
export default {
state,
getters,
mutations,
actions
}
关于上面代码一些命名(比如getters下面的getLogregShow,以及actions下面的setLogregShow,),这个命名完全看个人爱好,我觉得这样写能更好的让人理解get是获取XXX,set是设置XXX.
import Vue from 'vue'
import Vuex from 'vuex'
import logreg from './modules/logreg.js'
Vue.use(Vuex)
export default new Vuex.Store({
modules:{
logreg
}
})
<template>
<div>
<sign-in v-if="show===1"></sign-in>
<sign-up v-else-if="show===2"></sign-up>
<retrieve v-else-if="show===3"></retrieve>
</div>
</template>
<script>
import signIn from
import signUp from
import retrieve from
components:{
signIn,signUp,retrieve
}
</script>
mapActions({
showLogreg:'setLogregShow'
})
比如点击了登录那么 可以用
this.showLogreg(1)
就会弹出你的登录组件
新组建里面的show 也是通过getters映射到当前组件的
computed: {
...mapGetters({
show: 'getLogregShow'
})
},