搭建vueX目录架构(store)

index.js:(store入口)

import Vue from 'vue'
import Vuex from 'vuex'
import setting from "./modules/setting"
import user from "./modules/user"
import getters from "./getters";
Vue.use(Vuex)

const store = new Vuex.Store({
    modules: {
        setting,
        user
    },
    getters
})

export default store

getters.js:(通常可以将所有的getters放在外面方便获取;当然也可以写在modules下面js内部)

const getters={
    index:state=>state.setting.index
}
export default getters

setting.js

const state = {
    index:0
}
const mutations = {
    changeIndex_mutation(state, n) { /* n为角标 */
        state.index = n
    },
    hideIndex_mutation(state, n) {
        state.index = n
    }
}
const actions = {
    changeIndex(context, n) {
        context.commit('changeIndex_mutation', n)
    },
    hideIndex(context, n) {
        context.commit('hideIndex_mutation', n)
    }
}
// const getters = { //我们将所有的getters放到外面去
//     getStateIndex(state) {
//         return state.index
//     }
// }

export default {
    namespaced: true, //命名可重复
    state,
    mutations,
    actions,
    // getters
}

user.js(这里啥也没写,只做参考)

const state = {

}
const mutations = {

}
const actions = {

}
const getters = {

}
export default {
    namespaced: true, //命名可重复
    state,
    mutations,
    actions,
    getters
}

vue文件:

1:vueX_test.vue

<template>
    <div class="">
        <span class="ys-btn" @click="showIt(0)">显示蓝色的</span>
        <span class="ys-btn" @click="showIt(1)">显示红色的</span>
        <span class="ys-btn" @click="showIt(2)">显示黑色的</span>
        <span class="ys-btn" @click="removeAll(-1)">一个都不显示</span>
    </div>
</template>

<script>
    import {mapActions} from 'vuex';
    export default {
        name: "vueX_test",
        data(){
            return{

            }
        },
        mounted(){

        },
        methods:{
            // ...mapActions(['changeIndex','hideIndex']), //  未使用 namespaced: true, //命名可重复
            // showIt(i){ //这样传参
            //     this.changeIndex(i)
            // },
            // removeAll(i){ //这样传参
            //     this.hideIndex(i)
            // }

            ...mapActions('setting',['changeIndex','hideIndex']),
            showIt(i){ //这样传参
                this.changeIndex(i)
            },
            removeAll(i){ //这样传参
                this.hideIndex(i)
            }
        }

    }
</script>

2:vueX_test2.vue
 

<template>
    <div class="">
        <div class="ys-bg ys-bg-blue" :class="{'on':index==0}">1111</div>
        <div class="ys-bg ys-bg-red" :class="{'on':index==1}">222</div>
        <div class="ys-bg ys-bg-black" :class="{'on':index==2}">333</div>
    </div>
</template>

<script>
    import{mapState,mapGetters,mapActions} from 'vuex';
    export default {
        name: "vueX_test",
        data(){
            return{
                index:this.$store.getters.index //或者使用mapState
            }
        },
        // computed:{
        //     ...mapState({
        //         index:state=>state.setting.index
        //     })
        // }
    }
</script>

<style>
    .ys-bg{
        display: none;
        height:300px;
    }
    .ys-bg.on{
        display: block;
    }

</style>

注:

1:由上可见namespaced: true后 命名可重复,但是对应函数也变成局部函数,无法直接获取,需要设置对应文件路径;

2:... 是es6的展开运算符;mapAction返回数组对象(['c,'d']);将其直接结构到当前组数对象中;(['a','b',...mapAction]===['a','b','c','d'])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跃焱邵隼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值