在Vue3中体验Pinia 官网
安装好Pinia后,就可以在Vue3项目里使用它了。先在main.js里引入并使用Pinia。 import { createApp } from 'vue';import { createPinia } from 'pinia';import App from './App.vue';const app = createApp(App);const pinia = createPinia();app.use(pinia);app.mount('#app');// 运行结果:成功在Vue3项目中启用Pinia 然后创建一个store,比如创建一个counter store。 import { defineStore } from 'pinia';const useCounterStore = defineStore('counter', { state: () => ({ count: 0 }), actions: { increment() { this.count++; } }});// 运行结果:成功创建一个名为counter的store 在创建store时,要注意state的返回值是一个函数,这样能保证每个实例的状态独立。