2、使用Pinia

作者: 圆梦人生 | 来源:发表于2023-08-30 14:35 被阅读0次
  • 安装
npm install pinia
  • mian.js中 创建一个 pinia(根存储)
import { createApp } from 'vue'
import { createPinia } from 'pinia'

const app = createApp(App)
app.use(createPinia())
  • 定义一个Store(/src/store/goods/index.js)
import { defineStore } from "pinia";
//
export const goodStore = defineStore('goodStore', {
   state:()=>{
    return {
        goodcount: 100 
    }
   },
   //
   actions: {
    setGoodCount(){
        this.goodcount++
    }
   }
})
  • 使用stroe(src/views/goods/index.vue)
<script lang="ts" setup>
import { goodStore } from '@/store/goods/index'
const goodCount = goodStore();
goodCount.setGoodCount();
console.log('goodCount ==== ', goodCount.goodcount);

</script>

相关文章

网友评论

    本文标题:2、使用Pinia

    本文链接:https://www.haomeiwen.com/subject/abqamdtx.html