pinia基础使用

import {defineStore} from 'pinia'
import {StoreName} from './store-name'
// StoreName是id,用于区分不同的store,自定义即可
export const useStore = defineStore(StoreName.Test, {
  state: () => ({
    count: 0,
    sex: '顶顶顶'
  }),
  getters: {

  },
  actions: {
    increment() {
      this.count++
    },
  },
})
<template>
<div>
  直接通过实例.就可以获取
  {{ store.count }}---- {{ store.sex }}
</div>
</template>

<script setup lang='ts'>
import { useStore } from '../store'
// defineStore返回的是个hook,需要实例化
const store = useStore()
</script>

<style scoped lang='scss'>

</style>

 

posted on 2025-02-22 11:40  ChoZ  阅读(6)  评论(0)    收藏  举报

导航