使用vuex状态管理的modules进行分模块管理

情景:对于一个使用vue.js框架的大型的项目,如果项目中有好几个不同的业务模块,在数据共享的时候往往会使用vuex状态管理,在vuex中如果不进行模块化管理,store对象可能就变得非常臃肿,不仅不利于维护,也可能导致不同业务系统之间共享数据导致失误。

解决:独立维护一个公共仓库store,将一些各业务系统或项目共享的数据放到里面,然后根据业务系统单独维护一个store模块(module),每个模块单独维护自己的state、action、mutation、getter,最后注入到vue中

实践(使用vuex模块化开发)

store目录结构

  • store
  •   --moduleA
  •     --actions.js
  •     --getters.js
  •     --index.js
  •     --mutations.js
  •   actions.js
  •   getters.js
  •   index.js
  •   mutation-types.js
  •   mutations.js

在moduleA中的actions.js、getters.js、mutations.js文件中分别导出定义的方法

export default {

    fn (xxx,xxx) {

        xxx

    }

}

在moduleA中的index.js中先导入actions.js、getters.js、mutations.js文件中定义的方法,然后定义moduleA需要的state,最后export default 出去

import getters from './getters.js'

import mutations from './mutations .js'

import actions from './actions .js'

const state = {

}

export default {

    namespaced: true,   //使用命名空间

    state,

    getters,

    mutations,

    actions

}

在store的indexs.js文件中安装vuex并Vue.use(Vuex),然后new一个store实例,如下:

import Vue from 'vue'

import Vuex from 'vuex'

import getters from './getters.js'

import mutations from './mutations .js'

import actions from './actions .js'

import moduleA from './moduleA/index.js'

Vue.use(Vuex)

const state = {

}

export default new Vuex.Store({

    state,

    getters,

    mutations,

    actions,

    modules: {

        moduleA

    }

})

最后将store挂载到vue实例上

import store from './store/index.js'

new Vue({

    store,

    data: {

    },

    xxx

})

 

使用:假设在xxx.vue组件中

可以使用createNamespacedHelpers创建基于某个命名空间辅助函数。它返回一个对象,对象里有新的绑定在给定命名空间值上的组件绑定辅助函数。

import {createNamespacedHelpers} from 'vuex'

const moduleA = createNamespacedHelpers('moduleA')

或解构语法 const {mapState, mapGetters, mapMutatios} = createNamespacedHelpers('moduleA')
然后使用辅助函数,如

computed: {

    ...moduleA.mapGetters([

        'xxx'

    ])

}

采用解构方式:

computed: {

    ...mapGetters([

        'xxx'

    ])

}

如需要使用全局仓库store

import {mapState, createNamespacedHelpers} from 'vuex'

然后使用辅助函数,如

computed: {

    ...mapState([

        'xxx'

    ]),

    ...moduleA.mapGetters([

        'xxx'

    ])

}

其它辅助函数使用方式类似,结束了。。。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue、Vuex和TypeScript中使用modules进行模块化的方式如下: 首先,我们需要安装好Vue和Vuex,并使用TypeScript构建Vue项目。可以通过以下命令进行安装: ``` npm install vue vuex --save npm install typescript ts-loader --save-dev ``` 接下来,在根目录下创建`store`目录,并在该目录下创建`index.ts`文件。在`store`目录下再创建`modules`目录,并在该目录下创建对应的模块文件,例如`user.ts`和`product.ts`。 在`user.ts`模块中,定义用户相关的状态、mutations、actions和getters。例如: ```javascript // user.ts const user = { state: { userInfo: null }, mutations: { SET_USER_INFO(state, userInfo) { state.userInfo = userInfo; } }, actions: { setUserInfo({ commit }, userInfo) { commit('SET_USER_INFO', userInfo); } }, getters: { getUserInfo: state => state.userInfo } }; export default user; ``` 在`product.ts`模块中,同样定义商品相关的状态、mutations、actions和getters。 在`index.ts`文件中,引入Vue和Vuex,并创建一个Vuex Store对象,使用`module`方法将模块添加到Store中。例如: ```javascript // index.ts import Vue from 'vue'; import Vuex from 'vuex'; import user from './modules/user'; import product from './modules/product'; Vue.use(Vuex); const store = new Vuex.Store({ modules: { user, product } }); export default store; ``` 最后,在Vue的入口文件(如`main.ts`)中,引入创建的Vuex Store,并将其挂载到Vue实例上。例如: ```javascript // main.ts import Vue from 'vue'; import App from './App.vue'; import store from './store/index'; Vue.config.productionTip = false; new Vue({ store, render: h => h(App) }).$mount('#app'); ``` 现在,我们就可以在组件中使用`this.$store`来访问Vuex状态、提交mutations、触发actions等。在模块化的情况下,可以使用`this.$store.state.user.userInfo`来访问`user`模块中的`userInfo`状态使用`this.$store.commit('user/SET_USER_INFO', userInfo)`来提交`user`模块中的`SET_USER_INFO`mutation,使用`this.$store.dispatch('user/setUserInfo', userInfo)`来触发`user`模块中的`setUserInfo`action,以此类推。 通过以上的步骤,我们就可以在Vue、Vuex和TypeScript中使用modules进行模块管理应用的状态。这样可以使得代码结构更清晰,易于维护和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值