vuex的使用

vue3环境下的vux使用

  • 创建一个 store文件夹
  • store文件夹里分别创建 index.js与modules文件夹
  • 在 modules文件夹里创建想写的子模块文件,名称随意

index.js文件里的内容

js 复制代码
import { createStore } from 'vuex'
import examine from './modules/examine.js'

export default createStore({
  modules: {
    examine
  }
})

examine.js文件里的内容

js 复制代码
// 用户信息模块(局部模块)
export default {
  namespaced: true, // 开启命名空间
  state() {
    return {
      // 用户信息对象 
      user: {},
      // 侧边宽度
      asideWidth: "250px",
      menus: [],
      ruleNames: [],
      backgroundColor_header_Menu:'#000'

    }
  },
  mutations: {
    // 用户信息模块
    SET_USERINFO(state, value) {
      state.user = value
    },
    // 展开/缩起侧边
    handleAsideWidth(state) {
      state.asideWidth = state.asideWidth == "250px" ? "50px" : "250px"
    },
    // 左侧列表的数组
    SET_MENUS(state, menus) {
      state.menus = menus
    },
    //权限相关的数组
    SET_RULENAMES(state, ruleNames) {
      state.ruleNames = ruleNames
    }
  },
  actions: {
    GetMenuArray({commit},val){
      commit("SET_MENUS", val)
      commit("SET_USERINFO", {})
      commit("SET_MENUS", [])
    },
    
  }
}

至此,我们已经创建了一个仓库了,接下来是在文件中使用仓库

js 复制代码
<script setup>
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
const router = useRouter()
const route = useRoute()
const store = useStore()
console.log('可以看看打印',store)

// 这个是往仓库存数据
store.commit('examine/processData_VALUE', '我是存入的数据')
//这是读取仓库里的数据
const aa=store.state.examine.asideWidth
</script>
相关推荐
aircrushin5 分钟前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海2 小时前
JSBridge 原理详解
前端
孟健2 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion2 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计
IT枫斗者4 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
N***p3654 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
NGC_66115 小时前
二分查找算法
java·javascript·算法
享誉霸王5 小时前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5
a1117766 小时前
飞机躲避炸弹 网页游戏
前端·开源·html·threejs
夏乌_Wx6 小时前
mybash:简易 Shell 实现的设计思路与核心模块解析
linux·服务器·前端