vue3仓库模块并持久化
//store下的index.js
import {createPinia} from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const pinia=createPinia()
pinia.use(piniaPluginPersistedstate)
export default pinia
//组合式api
vue3定义分仓库代码 * 参数1:仓库的名字 * 参数2:函数(里面写核心代码) * 参数3:持久化配置 import { defineStore } from 'pinia' import { ref,computed } from 'vue' export const useUserstore = defineStore( 'user', () => { return {} }, { persist: true } )
// 选项式api
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => {
return { count: 0 }
},
actions: {
increment () {
this.count++
},
},
})
//组件中使用·
import { useUserstore } from '@/store/user'
userstore.setToken('')
//js/ts中使用(两句代码要写在一个地方)
const store = useUserstore()
store.setToken('')
浙公网安备 33010602011771号