|
|
@@ -4,16 +4,16 @@
|
|
|
<h3>初始值:{{ count }}--变大{{ bigCount }}</h3>
|
|
|
<h3>{{ hi }}--变大写{{ bigWord }}</h3>
|
|
|
<h3>
|
|
|
- <input type="text">
|
|
|
+ <input type="text" v-model.number="inpNum">
|
|
|
<br>
|
|
|
<br>
|
|
|
- <select>
|
|
|
+ <select v-model.number="num">
|
|
|
<option value="10">10</option>
|
|
|
<option value="15">15</option>
|
|
|
<option value="20">20</option>
|
|
|
</select>
|
|
|
</h3>
|
|
|
- <button>添加数值</button>
|
|
|
+ <button @click="handleNum">添加数值</button>
|
|
|
<button @click="handleAdd1">同步count+1</button>
|
|
|
<button @click="handleAdd2">异步count+2</button>
|
|
|
</div>
|
|
|
@@ -22,14 +22,28 @@
|
|
|
<script lang="ts" setup>
|
|
|
import {ref,computed} from "vue"
|
|
|
import { storeToRefs } from "pinia";
|
|
|
-import { useCountStore } from "@/store/count";
|
|
|
+import { useCountStore } from "@/store/count1";
|
|
|
const counter = useCountStore();
|
|
|
console.log(counter);
|
|
|
+// 修改方式3种
|
|
|
+// counter.count += 20; // 方式1
|
|
|
+
|
|
|
+// 批量修改
|
|
|
+// counter.$patch({
|
|
|
+// count: 777,
|
|
|
+// hi: 'hello World'
|
|
|
+// })
|
|
|
+
|
|
|
+// actions修改
|
|
|
+counter.asyncAddCount2(10);
|
|
|
+// console.log(counter.$onAction,'$$$$$')
|
|
|
let {count,bigCount,hi,bigWord} = storeToRefs(counter);
|
|
|
// let count = ref(5);
|
|
|
// let bigCount = computed(()=>{
|
|
|
// return count.value * 3;
|
|
|
// })
|
|
|
+let num = ref(0);
|
|
|
+let inpNum = ref(0);
|
|
|
function handleAdd1() {
|
|
|
console.log("同步count+1");
|
|
|
counter.addCount();
|
|
|
@@ -38,6 +52,40 @@ function handleAdd2() {
|
|
|
console.log("异步count+1");
|
|
|
counter.asyncAddCount();
|
|
|
}
|
|
|
+function handleNum() {
|
|
|
+ // console.log(num.value);
|
|
|
+ // console.log(count.value)
|
|
|
+ // if(num.value > 0){
|
|
|
+ // count.value += num.value
|
|
|
+ // }
|
|
|
+ // if(inpNum.value >0){
|
|
|
+ // count.value += inpNum.value
|
|
|
+ // }
|
|
|
+ // counter.asyncAddCount2(num.value)
|
|
|
+ // counter.count = 12;
|
|
|
+ // counter.$reset();
|
|
|
+ // console.log("重置")
|
|
|
+ counter.$dispose();
|
|
|
+}
|
|
|
+counter.$onAction(({ name,args,after, onError }) => {
|
|
|
+ console.log( name,args)
|
|
|
+ // 你可以在这里创建所有钩子之间的共享变量,
|
|
|
+ // 同时设置侦听器并清理它们。
|
|
|
+ after((resolvedValue) => {
|
|
|
+ console.log('action 1');
|
|
|
+ // 可以用来清理副作用
|
|
|
+ // `resolvedValue` 是 action 返回的值,
|
|
|
+ // 如果是一个 Promise,它将是已解决的值
|
|
|
+ })
|
|
|
+ onError((error) => {
|
|
|
+ // 可以用于向上传递错误
|
|
|
+ console.log('action 2');
|
|
|
+ })
|
|
|
+})
|
|
|
+counter.$subscribe((mutation, state) => {
|
|
|
+ console.log(mutation, state,'$subscribe');
|
|
|
+})
|
|
|
+
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss" scoped>
|