adong搬砖

导航

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('')
 
 

 

posted on 2023-11-26 01:47  adong搬砖  阅读(59)  评论(0)    收藏  举报