Skip to content

Commit bcadabc

Browse files
committed
feat[ptr]: 通过 vuex 进行共享数据管理
mutations 方法中不允许副作用操作 通过在 actions 中 dispatch 实现副作用操作
1 parent 615b5de commit bcadabc

9 files changed

Lines changed: 140 additions & 6 deletions

File tree

Vue2/sandboxs/package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Vue2/sandboxs/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
"test:TreeListMenu": "vue serve ./src/components/TreeListMenu/test.vue",
2121
"test:ListKey": "vue serve ./test/ListKey.vue",
2222
"test:LoadingButton": "vue serve ./src/components/LoadingButton/test.vue",
23-
"test:v-model":"vue serve ./test/v-model.vue"
23+
"test:v-model": "vue serve ./test/v-model.vue"
2424
},
2525
"dependencies": {
2626
"axios": "^1.13.4",
2727
"core-js": "^3.6.5",
2828
"mockjs": "^1.1.0",
2929
"querystring": "^0.2.1",
3030
"vue": "^2.6.11",
31-
"vue-router": "^3.6.5"
31+
"vue-router": "^3.6.5",
32+
"vuex": "^3.6.2"
3233
},
3334
"devDependencies": {
3435
"@vue/cli-plugin-babel": "~4.5.8",

Vue2/sandboxs/src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import { vLoading, vLazy } from './directives'
1414
Vue.directive("loading", vLoading)
1515
Vue.directive('lazy', vLazy)
1616

17+
import store from './store'
18+
1719
new Vue({
20+
store, // 注册 vuex 数据仓库
1821
router: router,
1922
render: h => h(App),
2023
}).$mount('#app')

Vue2/sandboxs/src/store/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// 数据仓库模块
2+
import Vuex from 'vuex'
3+
import Vue from 'vue'
4+
Vue.use(Vuex); // 注册 Vuex 插件到 Vue 中
5+
6+
import { delay } from '@/utils' // utils 是具名导出的
7+
8+
const store = new Vuex.Store({
9+
// 仓库配置对象
10+
state: {
11+
count: 0
12+
},
13+
mutations: { // 配置状态变更方法:监控状态变更
14+
increase(state) { // state 是默认参数,自动将当前状态传入
15+
state.count++; // 增加方法:在当前状态count+1
16+
},
17+
decrease(state) {
18+
state.count--;
19+
},
20+
power(state, payload) { // payload 负载,存储调用状态变更方法的额外数据
21+
// payload 为 幂次
22+
state.count = state.count ** payload
23+
},
24+
/*
25+
mutations 方法中不允许副作用操作
26+
因为 vuex 会在受到 commit 运行对应 mutation 方法之后立即记录状态,并不会等待事件循环运行副作用方法结束
27+
*/
28+
// sync(state){
29+
// setTimeout(() => {
30+
// state.count++;
31+
// },1000)
32+
// }
33+
},
34+
actions: {
35+
// 通过 actions 专门处理异步
36+
// 通过 dispatch 即 调用commit 实现
37+
async asyncIncrease(ctx) {
38+
await delay(1000)
39+
ctx.commit("increase")
40+
},
41+
async asyncDecrease(ctx) {
42+
await delay(1000)
43+
ctx.commit("decrease")
44+
},
45+
async asyncPower(ctx, payload) {
46+
await delay(1000)
47+
ctx.commit("power", payload)
48+
}
49+
}
50+
})
51+
52+
export default store

Vue2/sandboxs/src/utils/delay.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function delay(duration) {
2+
return new Promise((resolve) => {
3+
setTimeout(() => {
4+
resolve();
5+
}, duration);
6+
});
7+
}

Vue2/sandboxs/src/utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as toast } from './toast';
22
export { default as getComponentRootDom } from './getComponentRootDom';
3-
export { default as formatDate } from './formatDate'
3+
export { default as formatDate } from './formatDate'
4+
export { default as delay } from './delay'
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
<template>
2-
<h1>About ceilf6</h1>
2+
<div class="container">
3+
<h1>About ceilf6</h1>
4+
<A />
5+
<B />
6+
</div>
37
</template>
48

59
<script>
6-
export default {};
10+
import A from "../../../test/A.vue";
11+
import B from "../../../test/B.vue";
12+
13+
export default {
14+
components: {
15+
A,
16+
B,
17+
},
18+
};
719
</script>

Vue2/sandboxs/test/A.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div class="block">
3+
<p>the number is</p>
4+
<h1>{{ $store.state.count }}</h1>
5+
<!-- Vuex 配置后注册到了全局所有组件实例的 $store 属性中 -->
6+
<p>
7+
<button @click="handleDecrease">减少</button>
8+
<button @click="handleAsyncIncrease">异步增加</button>
9+
</p>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
methods: {
16+
handleDecrease() {
17+
this.$store.commit("decrease");
18+
},
19+
handleAsyncIncrease() {
20+
this.$store.dispatch("asyncIncrease");
21+
},
22+
},
23+
};
24+
</script>

Vue2/sandboxs/test/B.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div class="block">
3+
<p>the number is</p>
4+
<h1>{{ $store.state.count }}</h1>
5+
<p>
6+
<button @click="handleAsyncDecrease">异步减少</button>
7+
<button @click="handleIncrease">增加</button>
8+
</p>
9+
</div>
10+
</template>
11+
12+
<script>
13+
export default {
14+
methods: {
15+
handleIncrease() {
16+
this.$store.commit("increase");
17+
},
18+
handleAsyncDecrease() {
19+
this.$store.dispatch("asyncDecrease");
20+
},
21+
},
22+
};
23+
</script>
24+

0 commit comments

Comments
 (0)