Pinia
对比Vuex
Get Started
npm init vite npm i piniaimport { createPinia } from 'pinia' createApp(App).use(createPinia()).mount('#app')import { defineStore } from 'pinia' import { useOtherStore } from './other' export const useCounterStore = defineStore('counter', { state: () => { return { helloWorld: 'helloWorld', count: 0, phone: 18088881234, } }, //cache getters: { phoneHidden() { return this.phone.toString().replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') }, }, actions: { changeState() { this.count++ }, getOtherStore() { console.log(useOtherStore().prop) }, async fillData() { this.products = (await import("@/data/product.json")).default } }, })
修改state五种方式
$reset、$subscribe、$onAction
注意
Last updated