Vuex 以及 Pinia 中模块化以及命名空间的使用

1 篇文章 0 订阅

前言

之前开发过 dvajs ,发现 dvajs 中大多数都是用类似 dispatch('xx/xxx') 这种有命名空间 的开发模式,并且如果一直把所有 store 往一个项目里面堆会导致 store 难以维护 , 所以采用 模块化的store 来进行开发

模块

在这里插入图片描述

代码

menu/index.js

const MenuStore = {
  namespaced: true,
  state: {
    menu: []
  },
  getters: {
  },
  mutations: {
    setMenu (state, payload) {
      state.menu = payload
    }
  },
  actions: {
    setMenu (context, payload) {
      setTimeout(() => {
        context.commit('setMenu', payload)
      }, 1000)
    }
  }
}
export default MenuStore

user/index.js

const UserStore = {
  namespaced: true,
  state: {
    user: {}
  },
  getters: {
  },
  mutations: {
    setUser (state, payload) {
      state.user = payload
    }
  },
  actions: {
    setUser (context, payload) {
      setTimeout(() => {
        context.commit('setUser', payload) // 通过context去触发mutions中的sum
      }, 1000)
    }
  }
}
export default UserStore

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import UserStore from './user'
import MenuStore from './menu'
Vue.use(Vuex)
const store = new Vuex.Store({
  modules: {
    'menu': MenuStore,
    'user': UserStore
  }
})
export default store

具体页面调用

<template>
  <div class="hello">
    <ul>
      <li
        v-for="(v,i) in getMenu"
        :key="i"
      >
        {{v}}
      </li>
    </ul>
    <button @click="changeMenuMutations">点击我改变 menu (mutations) </button>
    <button @click="changeMenuActions">点击我改变 menu (actions) </button>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from 'vuex'
export default {
  name: 'Vuex',
  data () {
    return {
      arr: []
    }
  },
  mounted () { },
  methods: {
    ...mapMutations({
      setMenu: 'menu/setMenu'
    }),
    ...mapActions({
      setMenuActions: 'menu/setMenu'
    }),
    changeMenuMutations () {
      this.arr.push('菜单(Mutations)导入' + this.arr.length)
      //this.$store.commit('menu/setMenu', this.arr)
      this.setMenu(this.arr)
    },
    changeMenuActions () {
      this.arr.push('菜单(Actions)导入' + this.arr.length)
      // 相当于 this.$store.dispatch('menu/setMenu', this.arr)
      this.setMenuActions(this.arr)
    }
  },
  computed: {
    ...mapState({
      // 相当于 // this.$store.state.menu.menu
      getMenu: state => state.menu.menu,
      getUser: state => state.user.user
    })
  }
}
</script>

9.22 更新

pinia 模块化和命名空间使用

pinia 介绍

Pinia 最初是在 2019 年 11 月左右重新设计使用 Composition API 。从那时起,最初的原则仍然相同,但 Pinia 对 Vue 2 和 Vue 3 都有效,并且不需要您使用组合 API。

官网: pinia官网

项目目录

在这里插入图片描述

新建 modules => common.js

import { defineStore } from "pinia"
import { getStore } from "@/utils/storage"
const useCommonStore = defineStore("/common", {
  state: () => ({
    yearNow: new Date(),
  }),
  getters: {},
  actions: {},
})
export default useCommonStore

新建 modules => user.js

import { defineStore } from "pinia"

const useAuthUserStore = defineStore("/user", {
  state: () => ({
    userInfo: JSON.parse(window.localStorage.getItem("userInfo") || "{}"),
    isLogged: false,
  }),
  getters: {},
  actions: {
    setUserInfo() {
       console.log("getUserInfo")
    },
  },
})
export default useAuthUserStore

新建 index.js

import useAuthUserStore from "./modules/user"
import useCommonStore from "./modules/common"

export { useAuthUserStore, useCommonStore }

使用方式

<script setup>
import { useAuthUserStore } from "@/store/index"
const { setUserInfo } = useAuthUserStore()

setUserInfo() // getUserInfo
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值