Skip to content

Commit 0313340

Browse files
committed
feat[ptr]: pinia 的两种风格设置仓库
1 parent b3a9561 commit 0313340

6 files changed

Lines changed: 170 additions & 0 deletions

File tree

Vue3/sandboxs-vite/src/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ const app = createApp(App)
2121
import router from './router'
2222
app.use(router) // 使用插件
2323

24+
import { createPinia } from 'pinia'
25+
const pinia = createPinia()
26+
app.use(pinia)
27+
2428
app.mount('#app')
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 仓库文件
2+
import { defineStore } from "pinia";
3+
import delay from "../utils/delay";
4+
5+
// 第二个参数支持两种风格:options api 以及 composition api
6+
export const useCounterStore = defineStore("counter", {
7+
// 目前是 选项式API 风格
8+
state: () => {
9+
return {
10+
num: 0,
11+
};
12+
},
13+
getters: {
14+
// 类似于 计算属性
15+
// 针对上面 state 的数据做一个二次计算
16+
doubleNum: (state) => state.num * 2,
17+
},
18+
actions: {
19+
// 同步方法
20+
increment() {
21+
this.num++;
22+
},
23+
decrement() {
24+
this.num--;
25+
},
26+
// 异步方法
27+
async asyncIncrement() {
28+
await delay(1000)
29+
this.increment();
30+
},
31+
async asyncDecrement() {
32+
await delay(1000)
33+
this.decrement();
34+
},
35+
},
36+
});
37+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineStore } from 'pinia'
2+
import { reactive } from 'vue'
3+
4+
export const useTodoListStore = defineStore('list',
5+
// 组合式 风格
6+
() => {
7+
const list = reactive({
8+
items: [
9+
{
10+
text: "学习Pinia",
11+
isCompleted: false
12+
},
13+
{
14+
text: "任务2",
15+
isCompleted: true
16+
}
17+
]
18+
})
19+
20+
function addItem(newItemStr) {
21+
list.items.push({
22+
text: newItemStr,
23+
isCompleted: false
24+
})
25+
}
26+
27+
return { list, addItem }
28+
}
29+
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<template>
2+
<div class="container">
3+
<!-- <button class="plus" @click="num++">+</button> -->
4+
<!-- 不推荐直接操控数据,而是应该用仓库暴露的操控方法 -->
5+
<button class="plus" @click="increment">+</button>
6+
7+
<div class="num">{{ num }}</div>
8+
<button class="reduce" @click="decrement">-</button>
9+
<div class="doubleNum">{{ doubleNum }}</div>
10+
11+
<button class="asyncIncrement" @click="asyncIncrement">异步增加</button>
12+
<button class="asyncDecrement" @click="asyncDecrement">异步减少</button>
13+
14+
<button class="reset" @click="store.$reset">重置</button>
15+
<!-- 仓库实例属性 $reset 实现重置仓库状态 -->
16+
17+
<input type="text" v-model="newNum" />
18+
<button class="bind" @click="handleBind">绑定</button>
19+
</div>
20+
</template>
21+
22+
<script setup>
23+
import { useCounterStore } from "../../store/useCounterStore.js";
24+
import { storeToRefs } from "pinia";
25+
import { ref } from "vue";
26+
const store = useCounterStore();
27+
28+
// 解构数据
29+
const { num, doubleNum } = storeToRefs(store);
30+
// 如果不用 storeToRefs 仓库中的数据 breaks reactivity 不是响应式的
31+
32+
// 解构方法
33+
const { increment, decrement, asyncIncrement, asyncDecrement } = store;
34+
35+
const newNum = ref(""); // 用于和输入框做双向绑定
36+
37+
function handleBind() {
38+
store.$patch({
39+
// 仓库实例属性 $patch 可以用于变更仓库状态
40+
num: ~~newNum.value,
41+
// ~~ 用于快速转为32位整数
42+
});
43+
newNum.value = "";
44+
}
45+
</script>
46+
47+
<style scoped></style>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<template>
2+
<div class="container">
3+
<div class="add">
4+
<input type="text" v-model="newItem" />
5+
<button @click="handleAdd">添加</button>
6+
</div>
7+
8+
<div class="curList">
9+
<div v-for="(item, idx) in list.items" :key="idx">
10+
<div>{{ item.text }} {{ item.isCompleted ? "完成" : "未完成" }}</div>
11+
</div>
12+
</div>
13+
</div>
14+
</template>
15+
16+
<script setup>
17+
import { storeToRefs } from "pinia";
18+
import { useTodoListStore } from "../../store/useTodoListStore";
19+
const store = useTodoListStore();
20+
const { list } = storeToRefs(store);
21+
const { addItem } = store;
22+
23+
import { ref } from "vue";
24+
const newItem = ref("");
25+
function handleAdd() {
26+
if (!newItem.value) return;
27+
// list.value.items.push({
28+
// // list 是 ref 得取一层 value
29+
// text: newItem.value,
30+
// isCompleted: false,
31+
// });
32+
addItem(newItem.value); // 别忘记对 ref 取 value
33+
newItem.value = "";
34+
}
35+
</script>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div>
3+
<Counter></Counter>
4+
<TodoList></TodoList>
5+
</div>
6+
</template>
7+
8+
<script>
9+
import Counter from "./Counter.vue";
10+
import TodoList from "./TodoList.vue";
11+
12+
export default {
13+
components: {
14+
Counter,
15+
TodoList,
16+
},
17+
};
18+
</script>

0 commit comments

Comments
 (0)