pinia状态管理
pinia 是下一代的状态管理库,比 vuex 更简单,ts 支持更好。
你可以在 src/stores 中进行状态的定义。
例如创建 src/stores/counter.ts 👇
ts
// src/stores/counter.ts
import { defineStore } from "pinia";
export const useCounterStore = defineStore("counter", {
state() {
return { count: 0 };
},
actions: {
inc() {
this.count++;
},
},
});定义完后在 setup 中直接使用即可
html
<!-- src/pages/index.vue -->
<script setup lang="ts">
const Counter = useCounterStore()
<script>
<template>
<div @click="Counter.inc">{{Counter.count}}</div>
</template>更多具体使用可见 👉 pinia
