vue3 使用vuex Module

本文介绍了如何在 Vuex 中设置和使用模块(module),通过 `namespaced: true` 开启命名空间,使得状态管理和突变在不同模块间隔离。在 `index.js` 中导入并注册模块,如 `bar` 和 `horizontal`,每个模块包含其自身的状态、突变和命名空间。在组件中,可以利用 `useStore` 获取 store 并通过命名空间访问和修改状态,如 `store.state.bar.options.grid.left` 和 `store.commit('horizontal/setOptions', options.value)`。此示例展示了 Vuex 模块化组织和操作状态的方式。

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

vuex官网关于module

根据官网安装vux

npm install vuex@next --save

新建store文件

目录结构
index.js

import { createStore } from 'vuex'
import {merge} from "lodash";
// Create a new store instance.
import bar from './bar';
import horizontal from "@/store/horizontal";
const store = createStore({
    modules: {
        bar:bar,
        horizontal:horizontal
    }
})
export default store;

bar.js

import {merge} from "lodash";

const bar = {
    namespaced: true,
    state() {
        return {
            options: {
                color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4", "#ea7ccc"],
                grid: {
                    left: '10%',
                    right: '10%',
                    top: '10%',
                    bottom: '10%'
                }

            },
        }
    },
    mutations: {
        // 设置options
        setOptions(state, options) {
            state.options = merge({}, state.options, options)
        },
    }
}
export default bar;

horizontal.js

import {merge} from "lodash";
const horizontal={
  namespaced: true,
    state() {
        return {
            options: {
                color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4", "#ea7ccc"],
                grid: {
                    left: '10%',
                    right: '10%',
                    top: '10%',
                    bottom: '10%'
                }

            },
        }
    },
    mutations: {
        // 设置options
        setOptions(state, options) {
            state.options = merge({}, state.options, options)
        },
    }
};

export default horizontal;

main.js

const app = createApp(App)
//引入router
import router from "./router";
app.use(router)

组件中使用

import {useStore} from "vuex";
const store=useStore();

// 获取state
//bar
let barLeft=store.state.bar.options.grid.left;
//horizontal
let horizontalLeft=store.state.horizontal.options.grid.left;

// mution
//horizontal
// horizontal/setOptions  horizontal:命名空间 setOptions:mutation处理函数名称
store.commit('horizontal/setOptions',options.value);

简单来说就是使用 namespaced: true,开启别名,state根据对象嵌套访问。
其他属性 加命名空间 。官网实例

 modules: {
    account: {
      namespaced: true,

      // 模块内容(module assets)
      state: () => ({ ... }), // 模块内的状态已经是嵌套的了,使用 `namespaced` 属性不会对其产生影响
      getters: {
        isAdmin () { ... } // -> getters['account/isAdmin']
      },
      actions: {
        login () { ... } // -> dispatch('account/login')
      },
      mutations: {
        login () { ... } // -> commit('account/login')
      },
  }
}
Vue 3使用Vuex,你需要进行一些配置和使用方法上的改变。下面是使用Vuex的一般步骤: 1. 安装Vuex:在项目根目录下运行以下命令安装Vuex: ``` npm install vuex ``` 2. 创建store实例:在项目的src目录下创建一个新的文件夹,命名为store。在store文件夹中创建一个新的文件index.js,作为Vuex的入口文件。 3. 在index.js中导入VueVuex,并创建一个新的store实例: ```javascript import { createApp } from 'vue' import { createStore } from 'vuex' const store = createStore({ // 在这里定义你的状态、mutations、actions等 }) export default store ``` 4. 在main.js中引入store实例并将其挂载到Vue应用上: ```javascript import { createApp } from 'vue' import store from './store' const app = createApp(App) app.use(store) app.mount('#app') ``` 5. 在store文件夹中创建一个新的文件module.js,用于定义模块化的状态、mutations和actions。例如: ```javascript const state = { count: 0 } const mutations = { increment(state) { state.count++ } } const actions = { incrementAsync({ commit }) { setTimeout(() => { commit('increment') }, 1000) } } export default { state, mutations, actions } ``` 6. 在store/index.js中引入模块化的文件并注册到store实例中: ```javascript import { createStore } from 'vuex' import module from './module' const store = createStore({ modules: { module // 注册模块 } }) export default store ``` 现在你已经成功配置了Vuex,并可以在组件中使用它。你可以使用`$store`对象来访问状态、提交mutations和分发actions。例如,在组件中使用状态和提交mutation的示例: ```javascript export default { computed: { count() { return this.$store.state.module.count } }, methods: { increment() { this.$store.commit('increment') } } } ``` 这是一个简单的开始使用Vuex的例子,你可以根据你的具体需求来定义更多的状态、mutations和actions。请确保在使用之前阅读Vuex的文档以获取更多详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值