vuex的modules该如何使用哦
vuex构成
vuex主要包含以下五个部分:
- State // 存储变量、数据
- Getter // 类似计算属性
- Mutation // 唯一修改state的方法
- Action // 异步调用Mutation
- Module // 将store模块化
vuex的modules使用
创建目录

在此示例中,我创建了两个store文件,分别是 profile.js和custom.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

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

被折叠的 条评论
为什么被折叠?



