Vuex模块化(modules)与namespaced(命名空间)的搭配

Vuex模块化(modules)与namespaced(命名空间)的搭配

Vuex模块化(modules)格式

  • 原理:可以对Vuex的actions,mutations,state,getters四个属性综合成一个部分,做成一个模块使用
  • 优点:可以将一些特殊的功能独立,进行调用
javascript 复制代码
// store.js文件
const a = {
    actions : {
        方法名(context, value){
            ......对value的操作
            context.commit('mutations方法名', value)
        }
    },
    mutations : {
        方法名(state, value){
            ......对state更新,并赋值给value
        }
    },
    state : {
        对象名 : 100
    },
    getters : {
        方法名(state){
            return ......
        }
    }
}

export default new Vuex.Store({
    modules : {
        // 以下两种方法都行
        // 这个地方的写法是根据components组件的方式
        aModule : a
        // 也可以直接写成以下形式,跟 a : a 一样的效果
        a
    }
})

namespaced(命名空间)用法

  • 功能:用来控制dispatch和commit的全访问能力

注意点1:模块区分

  • 在模块化的基础上,对于dispatch或者是commit在调用时需要做好命名空间(namespaced)
    • 原因:在没有做模块化处理的时候,通过dispatch或者是commit调用时,是将一整个文件进行搜索,找寻与之对应的方法名。(做个小实验,如下)
javascript 复制代码
// A.vue
<template>
    <div>
        <button @click="A1">A1 BUTTON</button>
    </div>
</template>

<script>
    export default {
        name : 'A',
        methods : {
            A1(){
                this.$store.dispatch('A1')
            }
        }
    }
</script>
javascript 复制代码
// store.js文件
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const a = {
    actions : {
        A1(){
            console.log('A1 actions');
        }
    }
}

const b = {
    actions : {
        A1(){
            console.log('B1 actions');
        }
    }
}

export default new Vuex.Store({
    modules : {
        aModule : a,
        bModule : b,
    }
})
  • 以上store.js文件中,分别定义了a和b模块,在两个模块的actions中都有A1方法,执行效果如下:
  • 点击A1按钮,都触发了a和b模块中的A1方法。当我们只需要执行一个模块的时候,就可以使用命名空间(namespaced),在模块中的第一行加上namespaced : true,可以防止一次性全部访问到
javascript 复制代码
// store.js文件
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const a = {
    namespaced : true,
    actions : {
        A1(){
            console.log('A1 actions');
        }
    }
}

const b = {
    namespaced : true,
    actions : {
        A1(){
            console.log('B1 actions');
        }
    }
}

export default new Vuex.Store({
    modules : {
        aModule : a,
        bModule : b,
    }
})

注意点2:使用namespace模块区分后,在xxx.vue调用模块方法上要做出的改变

  • state:
    • 没有namespace:{{$store.state.对象名}}
    • 用了namespace:{{$store.state.模块名.对象名}}
  • actions:
    • 没有namespace:this.$store.dispatch('actions方法名', 值)
    • 用了namespace:this.$store.dispatch('模块名/actions方法名', 值)
  • mutations:
    • 没有namespace:this.$store.commit('mutations方法名', 值)
    • 用了namespace:this.$store.commit('模块名/mutations方法名', 值)
  • getters:
    • 没有namespace:{{$store.getters.getters方法名}}
    • 用了namespace:{{$store.getters['模块名/getters方法名']}}
javascript 复制代码
// xxx.vue
<template>
    <div>
        <h3>state:{{$store.state.aModule.对象名}}</h3>
        <h3>getters:{{$store.getters['aModule/getters方法名']}}</h3>
    </div>
</template>

<script>
    export default {
        name : 'xxx',
        methods : {
            方法名(){
                this.$store.dispatch('aModule/actions方法名', 值)
            },
            方法名(){
                this.$store.commit('aModule/mutations方法名', 值)
            }
        }
    }
</script>
javascript 复制代码
// store.js文件
const a = {
    namespaced : true,
    actions : {
        方法名(context, value){
            ......对value的操作
            context.commit('mutations方法名', value)
        }
    },
    mutations : {
        方法名(state, value){
            ......对state更新,并赋值给value
        }
    },
    state : {
        对象名 : 100
    },
    getters : {
        方法名(state){
            return ......
        }
    }
}

const b = {跟上面一样}

export default new Vuex.Store({
    modules : {
        // 以下两种方法都行
        // 这个地方的写法是根据components组件的方式
        aModule : a
        // 也可以直接写成以下形式,跟 a : a 一样的效果
        a
    }
})

导入导出式模板(简洁)

  • 将定义好的模块,封装在一个js文件中
javascript 复制代码
// store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 改成导入
import aModule from './a'

export default new Vuex.Store({
    modules : {
        aModule
    }
})
javascript 复制代码
// a.js导出
export default {
    namespaced : true,
    actions : {
        ......
    },
    mutations : {
        ......
    },
    state : {
        ......
    },
    getters : {
        ......
    }
}
相关推荐
万少8 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站10 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名13 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫13 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊13 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter13 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折13 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_13 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
Angelial13 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu14 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端