vuex的modules到底该如何使用?

本文介绍如何使用 Vuex 的模块化特性来组织状态管理代码。通过创建 profile 和 customs 两个模块,展示如何定义 state、mutations、actions 和 getters,并通过 mapActions 和 mapGetters 在 Vue 组件中使用这些模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vuex的modules该如何使用哦

vuex构成

vuex主要包含以下五个部分:

  • State // 存储变量、数据
  • Getter // 类似计算属性
  • Mutation // 唯一修改state的方法
  • Action // 异步调用Mutation
  • Module // 将store模块化

【VUEX】

vuex的modules使用

创建目录

在这里插入图片描述

在此示例中,我创建了两个store文件,分别是 profile.jscustom.js,一个根文件index.js

custom.js

const customs = {
    namespaced: true, // 创建命名空间
    state: { // 存储变量
        showAlert: false
    },
    mutations: { // 定义修改state方法
        CHANGESHOW: (state, params) => {
            state.showAlert = !state.showAlert
        }
    },
    actions: { // 异步调用mutations
        setShow: ({ commit }) => {
            commit('CHANGESHOW')
        }
    },
    getters: { // 将数据过滤输出
        bodyShow: state => state.showAlert
    }
}
export default customs

profile.js

const profile = {
  namespaced: true,
  state: {
    name: 'common name',
    age: 18,
    bool: false
  },
  mutations: {
    CHANGEMSG: (state, params) => {
      state.name = params
    },
    CHANGEAGE: (state, params) => {
      state.name = params
    },
    CHANGEBOOL: (state) => {
      state.bool = !state.bool
    }
  },
  actions: {
    setName: ({ commit }) => {
      commit('CHANGEMSG', 'Vuex common name')
    },
    setAge: ({ commit }) => {
      commit('CHANGEAGE', 81)
    },
    setBool: ({ commit }) => {
      commit('CHANGEBOOL')
    }
  },
  getters: {
    vuexName: state => state.name,
    vuexAge: state => state.age,
    vuexBool: state => state.bool
  }
}

export default common

index.js

import Vue from 'vue'
import Vuex from 'vuex'

// 引入子store
import profile from './modules/profile'
import customs from './modules/customs'

// Vue.use(Vuex)
const store = new Vuex.Store({
  modules: {
    profile,
    customs
  }
})

export default store // 导出store,以便于后续使用

在需要使用的.vue文件里进行使用。方法如下

index.vue

<template>
	<div>
	  name: <h5>{{vuexName}}</h5> <button @click='setName'>chenge name</button>
      age: <h5>{{vuexAge}}</h5> <button @click='setAge'>chenge age</button>
      bool: <h5>{{vuexBool}}</h5> <button @click='setBool'>chenge bool</button>
      <br/>
      
      <span @click='setShow' style='display:inline-block;width:200px;height:30px;border:1px solid #999;border-radius:5px;text-align:center;line-height:30px;cursor: pointer;'>click me ,change showAlert</span>
      <em>{{bodyShow}}</em>
	</div>
</template>
<script>
import { mapActions, mapGetters } from 'vuex'
export default {
computed: {
    ...mapGetters('profile', ['vuexName', 'vuexAge', 'vuexBool']),
    ...mapGetters('customs', ['bodyShow'])
  },
methods: {
    ...mapActions('customs', ['setShow']),
    ...mapActions('profile', ['setName', 'setAge', 'setBool']),
}
</script>
<style>

</style>

app.js

import Vue from 'vue';
import VueRouter from 'vue-router';
// style
import './../../sass/app.scss';

// Components
import Main from './Main.vue';
import routes from './routes';
// store
import store from './store';  // 将store挂载到Vue

Vue.use(VueRouter);

const router = new VueRouter({
  routes,
  saveScrollPosition: true,
});

new Vue({ router, store, ...Main }).$mount('#app');

初始效果图 ⬇️
在这里插入图片描述
点击按钮之后效果图 ⬇️
在这里插入图片描述
至此,modules使用流程演示完毕!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值