Vuex Module-状态仓库分割的使用介绍
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, 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使用流程演示完毕!【相关推荐:vue.js视频教程】
以上是Vuex Module-状态仓库分割的使用介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Vue2.x是目前最流行的前端框架之一,它提供了Vuex作为管理全局状态的解决方案。使用Vuex能够使得状态管理更加清晰、易于维护,下面将介绍Vuex的最佳实践,帮助开发者更好地使用Vuex以及提高代码质量。1.使用模块化组织状态Vuex使用单一状态树管理应用的全部状态,将状态从组件中抽离出来,使得状态管理更加清晰易懂。在具有较多状态的应用中,必须使用模块

在Python的开发过程中,经常会遇到找不到模块的错误。这个错误的具体表现就是Python在导入模块的时候报出ModuleNotFoundError或者ImportError这两个错误之一。这种错误很困扰,会导致程序无法正常运行,因此在这篇文章里,我们将会探究这个错误的原因及其解决方法。ModuleNotFoundError和ImportError在Pyth

Vuex是做什么的?Vue官方:状态管理工具状态管理是什么?需要在多个组件中共享的状态、且是响应式的、一个变,全都改变。例如一些全局要用的的状态信息:用户登录状态、用户名称、地理位置信息、购物车中商品、等等这时候我们就需要这么一个工具来进行全局的状态管理,Vuex就是这样的一个工具。单页面的状态管理View–>Actions—>State视图层(view)触发操作(action)更改状态(state)响应回视图层(view)vuex(Vue3.
![在Vue应用中使用vuex时出现“Error: [vuex] do not mutate vuex store state outside mutation handlers.”怎么解决?](https://img.php.cn/upload/article/000/000/164/168760467048976.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
在Vue应用中,使用vuex是常见的状态管理方式。然而,在使用vuex时,我们有时可能会遇到这样的错误提示:“Error:[vuex]donotmutatevuexstorestateoutsidemutationhandlers.”这个错误提示是什么意思呢?为什么会出现这个错误提示?如何解决这个错误?本文将详细介绍这个问题。错误提示的含
![在Vue应用中使用vuex时出现“Error: [vuex] unknown action type: xxx”怎么解决?](https://img.php.cn/upload/article/000/887/227/168766615217161.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
在Vue.js项目中,vuex是一个非常有用的状态管理工具。它可以帮助我们在多个组件之间共享状态,并提供了一种可靠的方式来管理状态的变化。但在使用vuex时,有时会遇到“Error:[vuex]unknownactiontype:xxx”的错误。这篇文章将介绍该错误的原因及解决方法。1.错误原因在使用vuex时,我们需要定义一些actions和mu

在Java9版本中Java语言引入了一个非常重要的概念:模块(module)。如果对javascript代码模块化管理比较熟悉的小伙伴,看到Java9的模块化管理,应该有似曾相识的感觉。一、什么是Javamodule?与Java中的package有些类似,module引入了Java代码分组的另一个级别。每个这样的分组(module)都包含许多子package包。通过在一个模块的源代码文件package的根部,添加文件module-info.java来声明该文件夹及其子文件夹为一个模块。该文件语法

在Vue应用中使用Vuex是非常常见的操作。然而,偶尔在使用Vuex时会遇到错误信息“TypeError:Cannotreadproperty'xxx'ofundefined”,这个错误信息的意思是无法读取undefined的属性“xxx”,导致了程序的错误。这个问题其实产生的原因很明显,就是因为在调用Vuex的某个属性的时候,这个属性没有被正确
