Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
unidocs-zh
提交
399a5091
unidocs-zh
项目概览
DCloud
/
unidocs-zh
通知
3599
Star
108
Fork
921
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
120
列表
看板
标记
里程碑
合并请求
109
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
unidocs-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
120
Issue
120
列表
看板
标记
里程碑
合并请求
109
合并请求
109
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
399a5091
编写于
9月 01, 2022
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update(vue3-vuex): 优化不需要特殊处理未初始化属性
上级
01ababaf
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
29 addition
and
53 deletion
+29
-53
docs/tutorial/vue3-vuex.md
docs/tutorial/vue3-vuex.md
+29
-53
未找到文件。
docs/tutorial/vue3-vuex.md
浏览文件 @
399a5091
# 状态管理Vuex
# 状态管理Vuex
> 这是与 Vue 3 匹配的 Vuex 4 的文档。差异对比可参阅[从 3.x 迁移到 4.0](https://next.vuex.vuejs.org/zh/guide/migrating-to-4-0-from-3-x.html)
> 这是与 Vue 3 匹配的 Vuex 4 的文档。差异对比可参阅[从 3.x 迁移到 4.0](https://next.vuex.vuejs.org/zh/guide/migrating-to-4-0-from-3-x.html)
>
>
> 已经有 Vue2项目,需要适配 Vue3 的可参阅 [vue2 项目迁移 vue3](https://uniapp.dcloud.io/migration-to-vue3)!
> 已经有 Vue2项目,需要适配 Vue3 的可参阅 [vue2 项目迁移 vue3](https://uniapp.dcloud.io/migration-to-vue3)!
...
@@ -210,7 +210,7 @@ export default store
...
@@ -210,7 +210,7 @@ export default store
2.
在
`main.js`
中导入文件。
2.
在
`main.js`
中导入文件。
```
js
```
js
// 页面路径:main.js
// 页面路径:main.js
import
App
from
'
./App
'
import
App
from
'
./App
'
import
store
from
'
./store
'
import
store
from
'
./store
'
import
{
createSSRApp
}
from
'
vue
'
import
{
createSSRApp
}
from
'
vue
'
...
@@ -243,7 +243,7 @@ export function createApp() {
...
@@ -243,7 +243,7 @@ export function createApp() {
},
},
computed
:
{
computed
:
{
username
()
{
username
()
{
return
store
.
state
.
username
return
store
.
state
.
username
}
}
}
}
}
}
...
@@ -266,7 +266,7 @@ export function createApp() {
...
@@ -266,7 +266,7 @@ export function createApp() {
},
},
computed
:
{
computed
:
{
username
()
{
username
()
{
return
this
.
$store
.
state
.
username
return
this
.
$store
.
state
.
username
}
}
}
}
}
}
...
@@ -300,7 +300,7 @@ export function createApp() {
...
@@ -300,7 +300,7 @@ export function createApp() {
// 从state中拿到数据 箭头函数可使代码更简练
// 从state中拿到数据 箭头函数可使代码更简练
username
:
state
=>
state
.
username
,
username
:
state
=>
state
.
username
,
age
:
state
=>
state
.
age
,
age
:
state
=>
state
.
age
,
})
})
}
}
</script>
</script>
```
```
...
@@ -348,11 +348,11 @@ export function createApp() {
...
@@ -348,11 +348,11 @@ export function createApp() {
return
{
return
{
firstName
:
"
Li
"
firstName
:
"
Li
"
}
}
},
},
computed
:
{
computed
:
{
...
mapState
({
...
mapState
({
username
:
function
(
state
)
{
username
:
function
(
state
)
{
return
this
.
firstName
+
'
'
+
state
.
username
return
this
.
firstName
+
'
'
+
state
.
username
},
},
age
:
state
=>
state
.
age
,
age
:
state
=>
state
.
age
,
})
})
...
@@ -437,7 +437,7 @@ export default store
...
@@ -437,7 +437,7 @@ export default store
-
getters, 等同于 store.getters
-
getters, 等同于 store.getters
```
js
```
js
// 页面路径:store/index.js
// 页面路径:store/index.js
import
{
createStore
}
from
'
vuex
'
import
{
createStore
}
from
'
vuex
'
const
store
=
createStore
({
const
store
=
createStore
({
...
@@ -481,7 +481,7 @@ export default store
...
@@ -481,7 +481,7 @@ export default store
```
html
```
html
<!-- 页面路径:pages/index/index.vue -->
<!-- 页面路径:pages/index/index.vue -->
<template>
<template>
<view>
<view>
<view
v-for=
"(item,index) in todos"
>
<view
v-for=
"(item,index) in todos"
>
<view>
{{item.id}}
</view>
<view>
{{item.id}}
</view>
<view>
{{item.text}}
</view>
<view>
{{item.text}}
</view>
...
@@ -510,7 +510,7 @@ export default store
...
@@ -510,7 +510,7 @@ export default store
```
html
```
html
<!-- 页面路径:pages/index/index.vue -->
<!-- 页面路径:pages/index/index.vue -->
<template>
<template>
<view>
<view>
<view
v-for=
"(item,index) in todos"
>
<view
v-for=
"(item,index) in todos"
>
<view>
{{item.id}}
</view>
<view>
{{item.id}}
</view>
<view>
{{item.text}}
</view>
<view>
{{item.text}}
</view>
...
@@ -549,7 +549,7 @@ export default store
...
@@ -549,7 +549,7 @@ export default store
export
default
{
export
default
{
computed
:
{
computed
:
{
todos
()
{
todos
()
{
return
this
.
$store
.
getters
.
getTodoById
(
2
)
return
this
.
$store
.
getters
.
getTodoById
(
2
)
}
}
}
}
}
}
...
@@ -657,7 +657,7 @@ export default store
...
@@ -657,7 +657,7 @@ export default store
</view>
</view>
</template>
</template>
<script>
<script>
import
store
from
'
@/store/index.js
'
import
store
from
'
@/store/index.js
'
export
default
{
export
default
{
computed
:
{
computed
:
{
count
()
{
count
()
{
...
@@ -685,7 +685,7 @@ export default {
...
@@ -685,7 +685,7 @@ export default {
-
在
`mutation`
传参(载荷)可以传递一个参数。
-
在
`mutation`
传参(载荷)可以传递一个参数。
```
js
```
js
// 页面路径:store/index.js
// 页面路径:store/index.js
import
{
createStore
}
from
'
vuex
'
import
{
createStore
}
from
'
vuex
'
const
store
=
createStore
({
const
store
=
createStore
({
state
:
{
state
:
{
...
@@ -730,7 +730,7 @@ export default store
...
@@ -730,7 +730,7 @@ export default store
```
js
```
js
// 页面路径:store/index.js
// 页面路径:store/index.js
import
{
createStore
}
from
'
vuex
'
import
{
createStore
}
from
'
vuex
'
const
store
=
createStore
({
const
store
=
createStore
({
state
:
{
state
:
{
...
@@ -869,27 +869,6 @@ const store = createStore({
...
@@ -869,27 +869,6 @@ const store = createStore({
export
default
store
export
default
store
```
```
**遵守规则**
既然
`Vuex`
的
`store`
中的状态是响应式的,那么当我们变更状态时,监视状态的
`Vue`
组件也会自动更新。这也意味着
`Vuex`
中的
`mutation`
也需要与使用
`Vue`
一样遵守一些注意事项:
-
最好提前在你的
`store`
中初始化好所有所需属性。
-
当需要在对象上添加新属性时,你应该
- 使用 `Vue.set(obj, 'newProp', 123)`, 或者
- 以新对象替换老对象。例如,利用对象展开运算符我们可以这样写:
```
js
state
.
obj
=
{
...
state
.
obj
,
newProp
:
123
}
```
**Mutation 必须是同步函数**
**Mutation 必须是同步函数**
一条重要的原则就是要记住
** mutation 必须是同步函数**
一条重要的原则就是要记住
** mutation 必须是同步函数**
...
@@ -998,7 +977,7 @@ export default store
...
@@ -998,7 +977,7 @@ export default store
```
js
```
js
// 页面路径:store/index.js
// 页面路径:store/index.js
import
{
createStore
}
from
'
vuex
'
import
{
createStore
}
from
'
vuex
'
const
store
=
createStore
({
const
store
=
createStore
({
state
:
{
state
:
{
...
@@ -1007,7 +986,7 @@ const store = createStore({
...
@@ -1007,7 +986,7 @@ const store = createStore({
mutations
:{
mutations
:{
add
(
state
,
payload
)
{
add
(
state
,
payload
)
{
state
.
count
+=
payload
.
amount
state
.
count
+=
payload
.
amount
}
}
},
},
actions
:{
actions
:{
addCountAction
(
context
,
payload
)
{
addCountAction
(
context
,
payload
)
{
...
@@ -1118,7 +1097,7 @@ export default store
...
@@ -1118,7 +1097,7 @@ export default store
注意我们正在进行一系列的异步操作,并且通过提交
`mutation`
来记录
`action`
产生的状态变更。
注意我们正在进行一系列的异步操作,并且通过提交
`mutation`
来记录
`action`
产生的状态变更。
#### mapActions
#### mapActions
2.
通过
`mapActions`
辅助函数分发。
2.
通过
`mapActions`
辅助函数分发。
...
@@ -1146,7 +1125,7 @@ export default store
...
@@ -1146,7 +1125,7 @@ export default store
},
},
methods
:
{
methods
:
{
...
mapActions
([
...
mapActions
([
'
addCountAction
'
,
'
addCountAction
'
,
// 将 `this.addCountAction()` 映射为 `this.$store.dispatch('addCountAction')`
// 将 `this.addCountAction()` 映射为 `this.$store.dispatch('addCountAction')`
])
])
}
}
...
@@ -1162,8 +1141,8 @@ export default store
...
@@ -1162,8 +1141,8 @@ export default store
```
js
```
js
methods
:
{
methods
:
{
...
mapActions
([
...
mapActions
([
'
addCountAction
'
'
addCountAction
'
// 将 `this.addCountAction(amount)` 映射为
// 将 `this.addCountAction(amount)` 映射为
//`this.$store.dispatch('addCountAction', amount)`
//`this.$store.dispatch('addCountAction', amount)`
]),
]),
}
}
...
@@ -1258,10 +1237,10 @@ export default store
...
@@ -1258,10 +1237,10 @@ export default store
```
html
```
html
├── components # 组件文件夹
├── components # 组件文件夹
└── myButton
└── myButton
└── myButton.vue # myButton组件
└── myButton.vue # myButton组件
├── pages
├── pages
└── index
└── index
└── index.vue # index页面
└── index.vue # index页面
├── static
├── static
├── store
├── store
...
@@ -1311,19 +1290,19 @@ export default createStore({
...
@@ -1311,19 +1290,19 @@ export default createStore({
4.
子模块
`moduleA`
页面内容。
4.
子模块
`moduleA`
页面内容。
```
js
```
js
// 子模块moduleA路径:store/modules/moduleA.js
// 子模块moduleA路径:store/modules/moduleA.js
export
default
{
export
default
{
state
:
{
state
:
{
text
:
"
我是moduleA模块下state.text的值
"
text
:
"
我是moduleA模块下state.text的值
"
},
},
getters
:
{
getters
:
{
},
},
mutations
:
{
mutations
:
{
},
},
actions
:
{
actions
:
{
}
}
}
}
```
```
...
@@ -1376,7 +1355,7 @@ export default {
...
@@ -1376,7 +1355,7 @@ export default {
</view>
</view>
</template>
</template>
<script>
<script>
import
{
mapState
,
mapGetters
}
from
'
vuex
'
import
{
mapState
,
mapGetters
}
from
'
vuex
'
export
default
{
export
default
{
computed
:
{
computed
:
{
...
mapState
({
...
mapState
({
...
@@ -1415,6 +1394,3 @@ export default {
...
@@ -1415,6 +1394,3 @@ export default {
```
```
vue是单向数据流,子组件不能直接修改父组件的数据,而通过vuex状态管理实现:把组件的共享状态抽取出来,以一个全局单例模式管理。在这种模式下,我们的组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为!
vue是单向数据流,子组件不能直接修改父组件的数据,而通过vuex状态管理实现:把组件的共享状态抽取出来,以一个全局单例模式管理。在这种模式下,我们的组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为!
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录