• 在src目录下创建stores文件夹。

  • 创建index.ts文件用于创建pinio

index.ts

import { createPinia } from 'pinia'
const pinia = createPinia()
export default pinia

main.ts引用

// 注册 Pinia
import pinia from './stores/index.ts'
app.use(pinia)
  • 创建各种store

useUserStore 一般以use开头Store结尾

import { defineStore } from 'pinia' //引入

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo: {}
  }),
  actions: {
    setToken(token: string) {
      this.token = token
    },
    setUserInfo(userInfo: any) {
      this.userInfo = userInfo
    }
  }
})