pinia 解构state

<template>
  <div @click="change">
    直接通过实例.就可以获取
    {{ store.count }}---- {{ store.sex }}
  </div>
  {{ count }}
  </template>
  
  <script setup lang='ts'>
  import { useStore } from '../store'
  // defineStore返回的是个hook,需要实例化
  const store = useStore()
  const change = () => {
    store.count++
  }

  import { storeToRefs } from 'pinia'
  // const { count } = store // 直接结构丧失响应式
  // 通过storeToRefs获取响应式,不用torefs是会把所有的属性都转成ref,浪费性能
  const { count } = storeToRefs(store) 

  </script>
  
  <style scoped lang='scss'>
  
  </style>

 

posted on 2025-02-22 14:55  ChoZ  阅读(8)  评论(0)    收藏  举报

导航