步骤:
根据官网操作
通过百度网络csdn查询他人所写,查缺补漏
store中index.js
import { defineStore } from 'pinia'
export const useStore = defineStore('storeId', {
// 推荐使用 完整类型推断的箭头函数
state: () => ({
counter: 90,
}),
actions: {
increment(msg) {
this.counter=msg
console.log(msg);
},
randomizeCounter(msg) {
this.counter = msg
},
},
})
入口函数main.js
步骤:
插件必须注册
import Vue from 'vue'
// import { createApp } from 'vue'
import App from './App.vue'
import { createPinia, PiniaVuePlugin } from 'pinia'
Vue.use(PiniaVuePlugin)
const pinia = createPinia()
new Vue({
render: h => h(App),
pinia,
}).$mount('#app')
存储与调用
//存储
import { useStore } from '@/store/index'
const store = useStore()
store.存储方法()
//调用
//与存储相同:导入+创建变量+调用方法
调用案例
