• vue3中如何使用Pinia


    Pinia 是 Vue 3 中的状态管理库,类似于 Vue 2 中的 Vuex。Pinia 的最大特点是使用了 Vue 3 的新特性,比如 Composition API 和 Proxy,提供了更好的性能和开发体验。

    相比之下,Vue 2 中的 Vuex 依赖于 Vue 2 的响应式系统,不支持 Composition API 和 Proxy。尽管 Vuex 可以使用 Vue 3,但是它仍然不会受益于 Vue 3 的新特性。

    此外,Pinia 还提供了 TypeScript 支持的 API,比 Vuex 更加类型安全。因此,如果正在使用 Vue 3,建议使用 Pinia 作为状态管理库。

    下面是vue3中使用pinia的实例代码:

    1. 定义store
    2. import {defineStore} from "pinia";
    3. export const useAlarmStore = defineStore('alarm', {
    4. state: () => (
    5. {
    6. message: 'hello',
    7. content: 'content',
    8. detail: 'detail',
    9. isFirst: 0
    10. }
    11. ),
    12. getters: {
    13. getMessage() {
    14. return this.message + 'con' + Math.random()
    15. }
    16. },
    17. actions: {
    18. handleMessageFun() {
    19. this.message = Math.random() + 'hello'
    20. this.content = Math.random() + 'content'
    21. },
    22. handleFirstFun() {
    23. this.isFirst = 1
    24. }
    25. },
    26. persist: {
    27. enabled: true,
    28. strategies: [
    29. {
    30. key: 'alarm', //存储了所有变量
    31. storage: localStorage
    32. // paths: ['message', 'content', 'isFirst']
    33. }
    34. // {storage: sessionStorage, paths: ['message']},
    35. // {storage: localStorage, paths: ['content']}
    36. ]
    37. }
    38. })
    39. 页面使用
    40. <script>
    41. import detail from "@/views/componetns/detail.vue";
    42. import {computed, onBeforeUnmount, onMounted, ref} from "vue";
    43. import {getAlarmData} from "@/api/jiankong";
    44. import {useAlarmStore} from '@/store/modules/alarm'
    45. import {storeToRefs} from "pinia";
    46. export default {
    47. components: {
    48. detail
    49. },
    50. setup(props, context) {
    51. const store = userAlarmStore()
    52. // 获取store 中的state 属性
    53. // console.log(store.message)
    54. //1) 获取store getters
    55. const showMess = computed(() => store.getMessage)
    56. console.log(showMess)
    57. // 获取store 中的方法
    58. function changeMessage() {
    59. // 获取actions 定义的方法
    60. store.handleMessageFun()
    61. // console.log(store.message)
    62. }
    63. // console.log(store.getMessage())
    64. //2)store 中存储的属性值直接取出来使用就不会有响应式了,所以使用storeToRefs 就会有响应式
    65. const {message} = storeToRefs(store)
    66. // 获取store 中变量的方法 可以使用12两种 都可以
    67. //以上是sore 使用-------------------------------------
    68. let res = getAlarmData()
    69. let tableData = res.data
    70. let msg = ref('')
    71. let showDetail = ref(false)
    72. function checkDetail(row) {
    73. showDetail.value = true
    74. msg.value = {...row}
    75. }
    76. function saveEmit(value) {
    77. console.log(value)
    78. }
    79. // function checkPoint(event) {
    80. // console.log(event.pageX)
    81. // console.log(event.pageY)
    82. // }
    83. // onMounted(() => {
    84. // window.addEventListener('click', checkPoint)
    85. // })
    86. // onBeforeUnmount(() => {
    87. // window.removeEventListener('click', checkPoint)
    88. // })
    89. return {
    90. tableData,
    91. checkDetail,
    92. showDetail,
    93. msg,
    94. saveEmit,
    95. message,
    96. changeMessage,
    97. showMess
    98. }
    99. }
    100. }
    101. </script>
    102. <template>
    103. <div class="table-content">
    104. <div @click="changeMessage">
    105. 新增{{ message }} ---------{{ showMess }}
    106. </div>
    107. <el-table
    108. :data="tableData"
    109. stripe
    110. border
    111. style="width: 100%"
    112. max-height="830"
    113. >
    114. <el-table-column
    115. prop="psName"
    116. label="企业名称"
    117. width="180"
    118. />
    119. <el-table-column
    120. prop="alarmRuleName"
    121. label="报警名称"
    122. width="180"
    123. show-overflow-tooltip
    124. >
    125. <template #default="scope">
    126. {{ scope.row.psName + scope.row.alarmRuleName }}
    127. </template>
    128. </el-table-column>
    129. <el-table-column
    130. prop="content"
    131. label="报警内容"
    132. />
    133. <el-table-column
    134. prop="content"
    135. width="90"
    136. label="操作"
    137. align="center"
    138. >
    139. <template #default="scope">
    140. <el-button
    141. text
    142. type="primary"
    143. @click="checkDetail(scope.row)"
    144. >
    145. 查看
    146. </el-button>
    147. </template>
    148. </el-table-column>
    149. </el-table>
    150. </div>
    151. <detail
    152. v-if="showDetail"
    153. :msg="msg"
    154. @saveEmit="saveEmit"
    155. />
    156. </template>
    157. <style scoped lang="scss">
    158. .table-content {
    159. margin: 15px;
    160. padding: 15px;
    161. background-color: #FFFFFF;
    162. border-radius: 2px;
    163. }
    164. </style>

  • 相关阅读:
    7.2 怎样定义函数
    Day06:运算符详解
    记录我在cmd里使用pip命令下载Python的包时碰见的两个错误
    linux进程间通信之信号
    Python 基于OpenCV+face_recognition实现人脸捕捉与人脸识别
    Java拷贝之深拷贝与浅拷贝
    每日一练 | 华为认证真题练习Day119
    现场直击|亚数TrustAsia精彩亮相IOTE深圳物联网展,CSA联盟展台等你来!
    【Python入门基础1】关于Pycharm编译器的配置
    Spring的IOC和AOP
  • 原文地址:https://blog.csdn.net/qq_37481512/article/details/134458855