You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/vuex/modules.md
+17-17Lines changed: 17 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,9 @@ order: 8
6
6
7
7
# Modules
8
8
9
-
Due to using a single state tree, all state of our application is contained inside one big object. However, as our application grows in scale, the store can get really bloated.
To help with that, Vuex allows us to divide our store into **modules**. Each module can contain its own state, mutations, actions, getters, and even nested modules - it's fractal all the way down:
11
+
为了解决这个问题,Vuex 允许我们把 store 分 module(模块)。每一个模块包含各自的状态、mutation、action 和 getter,甚至是嵌套模块, 如下就是它的组织方式:
12
12
13
13
```js
14
14
constmoduleA= {
@@ -35,16 +35,16 @@ store.state.a // -> moduleA's state
35
35
store.state.b// -> moduleB's state
36
36
```
37
37
38
-
### Module Local State
38
+
### 模块本地状态
39
39
40
-
Inside a module's mutations and getters, The first argument received will be **the module's local state**.
40
+
模块的 mutations 和 getters方法第一个接收参数是**模块的本地状态**。
41
41
42
42
```js
43
43
constmoduleA= {
44
44
state: { count:0 },
45
45
mutations: {
46
46
increment: (state) {
47
-
// state is the local module state
47
+
// state 是模块本地的状态。
48
48
state.count++
49
49
}
50
50
},
@@ -57,7 +57,7 @@ const moduleA = {
57
57
}
58
58
```
59
59
60
-
Similarly, inside module actions, `context.state`will expose the local state, and root state will be exposed as `context.rootState`:
Also, inside module getters, the root state will be exposed as their 3rd argument:
75
+
在模块的 getters 内,根状态也会作为第三个参数暴露。
76
76
77
77
```js
78
78
constmoduleA= {
@@ -85,15 +85,15 @@ const moduleA = {
85
85
}
86
86
```
87
87
88
-
### Namespacing
88
+
### 命名空间
89
89
90
-
Note that actions, mutations and getters inside modules are still registered under the **global namespace** - this allows multiple modules to react to the same mutation/action type. You can namespace the module assets yourself to avoid name clashing by prefixing or suffixing their names. And you probably should if you are writing a reusable Vuex module that will be used in unknown environments. For example, we want to create a `todos`module:
//define getters, actions and mutations using prefixed names
106
+
//用带前缀的名称来定义 getters, actions and mutations
107
107
consttodosModule= {
108
108
state: { todos: [] },
109
109
@@ -127,18 +127,18 @@ const todosModule = {
127
127
}
128
128
```
129
129
130
-
### Dynamic Module Registration
130
+
### 注册动态模块
131
131
132
-
You can register a module **after** the store has been created with the `store.registerModule` method:
132
+
你可以用 `store.registerModule` 方法在 store 创建**之后**注册一个模块:
133
133
134
134
```js
135
135
store.registerModule('myModule', {
136
136
// ...
137
137
})
138
138
```
139
139
140
-
The module's state will be exposed as `store.state.myModule`.
140
+
模块的 `store.state.myModule` 暴露为模块的状态。
141
141
142
-
Dynamic module registration makes it possible for other Vue plugins to also leverage Vuex for state management by attaching a module to the application's store. For example, the [`vuex-router-sync`](https://github.com/vuejs/vuex-router-sync)library integrates vue-router with vuex by managing the application's route state in a dynamically attached module.
You can also remove a dynamically registered module with `store.unregisterModule(moduleName)`. Note you cannot remove static modules (declared at store creation) with this method.
144
+
你也能用 `store.unregisterModule(moduleName)` 移除动态注册过的模块。但是你不能用这个方法移除静态的模块(也就是在 store 创建的时候声明的模块)。
Copy file name to clipboardExpand all lines: src/vuex/structure.md
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -6,33 +6,33 @@ order: 9
6
6
7
7
# Application Structure
8
8
9
-
Vuex doesn't really restrict how you structure your code. Rather, it enforces a set of high-level principles:
9
+
实际上,Vuex 在怎么组织你的代码结构上面没有任何限制,相反,它强制规定了一系列高级的原则:
10
10
11
-
1.Application-level state is centralized in the store.
11
+
1.应用级的状态集中放在 store 中。
12
12
13
-
2.The only way to mutate the state is by committing **mutations**, which are synchronous transactions.
13
+
2.改变状态的唯一方式是提交**mutations**,这是个同步的事务。
14
14
15
-
3.Asynchronous logic should be encapsulated in, and can be composed with **actions**.
15
+
3.异步逻辑应该封装在**action** 中。
16
16
17
-
As long as you follow these rules, it's up to you how to structure your project. If your store file gets too big, simply start splitting the actions, mutations and getters into separate files.
17
+
只要你遵循这些规则,怎么构建你的项目的结构就取决于你了。如果你的 store 文件非常大,仅仅拆分成 action、mutation 和 getter 多个文件即可。
18
18
19
-
For any non-trivial app, we will likely need to leverage modules. Here's an example project structure:
19
+
对于稍微复杂点的应用,我们可能都需要用到模块。下面是一个简单的项目架构:
20
20
21
21
```bash
22
22
├── index.html
23
23
├── main.js
24
24
├── api
25
-
│ └── ... #abstractions for making API requests
25
+
│ └── ... #这里发起 API 请求
26
26
├── components
27
27
│ ├── App.vue
28
28
│ └── ...
29
29
└── store
30
-
├── index.js #where we assemble modules and export the store
31
-
├── actions.js #root actions
32
-
├── mutations.js #root mutations
30
+
├── index.js #组合 modules 、export store
31
+
├── actions.js #根 action
32
+
├── mutations.js #根 mutations
33
33
└── modules
34
-
├── cart.js # cart module
35
-
└── products.js # products module
34
+
├── cart.js # cart 模块
35
+
└── products.js # products 模块
36
36
```
37
37
38
-
As a reference, check out the [Shopping Cart Example](https://github.com/vuejs/vuex/tree/dev/examples/shopping-cart).
0 commit comments