zmc
2023-09-04 34257f504330191b1a698eb48b52217095db47fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<script>
import {useCounterStore} from '@/stores/counter';
export default {
   data(){
      return{
 
      }
   },
   setup(){
    const store = useCounterStore()
    const unsubscribe = store.$onAction(
  ({
    name, // action 的名字
    store, // store 实例
    args, // 调用这个 action 的参数
    after, // 在这个 action 执行完毕之后,执行这个函数
    onError, // 在这个 action 抛出异常的时候,执行这个函数
  }) => {
    // 记录开始的时间变量
    const startTime = Date.now()
    // 这将在 `store` 上的操作执行之前触发
    console.log(`Start "${name}" with params [${args.join(', ')}].`)
 
    // 如果 action 成功并且完全运行后,after 将触发。
    // 它将等待任何返回的 promise
    after((result) => { 
      console.log(
        `Finished "${name}" after ${
          Date.now() - startTime
        }ms.\nResult: ${result}.`
      )
    })
 
    // 如果 action 抛出或返回 Promise.reject ,onError 将触发
    onError((error) => {
      console.warn(
        `Failed "${name}" after ${Date.now() - startTime}ms.\nError: ${error}.`
      )
    })
  }
)
    return{
      store,unsubscribe
    }
   },
   computed:{
    a(){
      return this.store.doubleCount*2
    },
 
   },
   mounted(){
 
   },
   methods:{
    doThing(){
      this.store.increment(5)
      this.store.doubleCount
    }
   }
}
</script>
 
<template>
  <div>
    <el-button type="primary" @click="doThing">点击{{ store.doubleCount }}</el-button>
  </div>
  <div>{{ a }}</div>
  <div>{{ store.secret }}</div>
  <div>{{ store.vue }}</div>
  <div>{{ store.天 }}</div>
 
</template>
<style scoped>
 
</style>