Detailed introduction to Chinese documents of vuex
Vuex is a state management model specially developed for Vue.js applications. It centrally stores and manages the state of all components of the application. This article introduces you to the vuex usage documentation. Friends who need it can refer to it
What is Vuex?
Vuex is a state management pattern developed specifically for Vue.js applications. It uses centralized storage to manage the state of all components of the application, and uses corresponding rules to ensure that the state changes in a predictable way. Vuex is also integrated into Vue's official debugging tool devtools extension, providing advanced debugging functions such as zero-configuration time-travel debugging, status snapshot import and export, etc.
Installation
Direct download CDN reference
1 2 |
|
npm
1 |
|
In a modular packaging system, you must install Vuex explicitly via Vue.use().
1 2 3 |
|
Vuex is a state management model developed specifically for Vue.js applications. It centrally stores and manages the state of all components of the application.
State management includes the following partial states:
State drives the data source of the application;
View maps state to views in a life-like manner.
actions Respond to state changes caused by user input on the view.
Help us manage shared status for medium and large single-page applications.
state
Single state tree, Vuex uses a single state tree to contain all application-level states with one object.
Get the Vuex state in the Vue component.
Since Vuex’s state storage is reactive, the easiest way to read the state from the store instance is to return a certain state in a computed property.
Create a Counter component
1 2 3 4 5 6 7 8 |
|
Every time store.state.count changes, the calculated properties will be re-obtained and the relevant DOM will be updated
Vuex passes the store option, provides a mechanism to "inject" state from the root component into each sub-component (requires calling Vue.use(Vuex)):
1 2 3 4 5 6 7 8 9 10 11 |
|
By registering the store option in the root instance, the store The instance will be registered to all sub-components under the component, and the sub-components can be accessed through this.$store. Update the implementation of counter:
1 2 3 4 5 6 |
|
mapState auxiliary function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
mapState Auxiliary Function
1 2 3 4 |
|
mapState Auxiliary Function
1 2 3 4 5 |
|
mapState Auxiliary Function
Attributes will be somewhat redundant. In order to solve this problem, we can use the mapState helper function to help us generate calculated properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 |
|
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 |
|
对象风格的提交方式
提交mutation 的另一种方式直接使用包含 type 属性的对象:
1 2 3 4 |
|
当使用对象风格的提交方式,整个对象作为载荷传给mutation 函数,因此handler保持不变:
1 2 3 4 5 |
|
Mutations 需遵守vue 的响应规则
既然Vuex的store 中的状态是响应式的,那么当我们变更状态时,监视状态的vue更新 ,这也意味值Vue 中的mutation 也需要与使用 Vue 一样遵守一些注意事项。
1. 最好提前在你的store 中初始化好所有的所需要的属性。
2.当需要在对象上提交新属性时,你应该使用
1 |
|
使用新对象代替老对象 state.obj= {...state.obj ,newProp: 123}
使用常量替代 Mutation 事件类型
使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式
1 2 3 4 5 6 7 8 9 10 11 12 |
|
mutation 必须是同步函数
一条重要的原则是记住 mutation 必须是同步函数。
1 2 3 4 5 6 7 |
|
在组件中提交 Mutations
你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使使用 mapMutations辅助函数将组建中的methods 映射为 store.commit 调用 (需要在根节点注入 store)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Actions
在mutation 中混异步调用会导致你的程序很难调试。
Actions
Action 类似于 mutation,不同在于。
Action 提交的是 mutation ,而不是直接变更状态。
Action 可以包含任意异步操作。
注册一个简单的 action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Action 函数接受一个与store 实例具有相同方法和属性的context 对象,因此你可以调用 context.commit 提交一个mutation,或者通过 context.state 和context.getters 来获取 state 和 getters 当我们在之后介绍到Modules时,你就知道 context 对象为什么不是store 实例本身了。
1 2 3 4 5 |
|
分发 Action
Action 通过 store.dispatch 方法触发:
1 |
|
我们可以在 action 内部执行异步操作。
1 2 3 4 5 6 7 |
|
Actions 支持同样的载荷方式和对象方式进行分发
1 2 3 4 5 6 7 8 9 |
|
在组件中分发 Action
你在组件中使用 this.$store.dispatch('xxx')
分发 action,或者使用map Actions辅助函数将组件的methods 映射为store.dispatch 调用
1 2 3 4 5 6 7 8 9 |
|
组合 Actions
Action 通常是异步的,那么如何知道 action 什么时候结束。
你需要明白 store.dispatch 可以处理被处触发的action 的回调函数返回的Promise并且 store.dispatch 仍旧返回Promise
1 2 3 4 5 6 7 8 9 10 |
|
现在你可以
1 2 3 |
|
在另一个 action 中也可以
1 2 3 4 5 6 7 |
|
我们利用async/ await
1 2 3 4 5 6 7 8 9 10 |
|
Modules
使用单一状态树,当应用变的很大的时候,store 对象会变的臃肿不堪。
Vuex 允许我们将store 分割到模块。每一个模块都有自己的state, mutation,action, getters, 甚至是嵌套子模块从上到下进行类似的分割。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
模块的局部状态
对于模块内部的 mutation 和 getter, 接收的第一个参数是模块的局部状态。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
同样对于模块内部的action, context.state 是局部状态,根节点的窗台石context.rootState:
1 2 3 4 5 6 7 8 9 |
|
对于模块内部的getter,跟节点状态会作为第三个参数:
1 2 3 4 5 6 7 8 9 |
|
命名空间
模块内部的action, mutation , 和 getter 现在仍然注册在全局命名空间 这样保证了多个模块能够响应同一 mutation 或 action. 也可以通过添加前缀 或者 后缀的
方式隔离各个模块,以免冲突。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
模块动态注册
在store 创建之后,你可以使用 store.registerModule 方法注册模块。
1 |
|
模块的状态将是 store.state.myModule.
模块动态注册功能可以使让其他Vue 插件为了应用的store 附加新模块
以此来分割Vuex 的状态管理。
项目结构
Vuex 并不限制你的代码结构。但是它规定了一些需要遵守的规则:
1.应用层级的状态应该集中到单个store 对象中。
2.提交 mutation 是更改状态的唯一方法,并且这个过程是同步的。
3.异步逻辑应该封装到action 里面。
只要你遵守以上规则,如何组织代码随你便。如果你的 store 文件太大,只需将 action、mutation、和 getters 分割到单独的文件对于大型应用,我们会希望把 Vuex 相关代码分割到模块中。下面是项目结构示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
The above is the detailed content of Detailed introduction to Chinese documents of vuex. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig
