| name | pinia-store-generator |
| description | 创建符合项目规范的 Pinia Store |
Pinia Store 生成器
快速创建符合 wot-starter 项目规范的 Pinia Store。
目录结构
src/store/
├── persist.ts # 持久化配置
├── themeStore.ts # 主题 store 示例
├── manualThemeStore.ts # 手动主题 store 示例
└── {moduleName}Store.ts # 新建的 store
Store 模板
Options Store(推荐)
import type { {ModuleName}State } from '@/types/{moduleName}'
import { defineStore } from 'pinia'
export const use{ModuleName}Store = defineStore('{moduleName}', {
state: (): {ModuleName}State => ({
data: null,
loading: false,
error: null,
}),
getters: {
isLoaded: (state) => state.data !== null,
hasError: (state) => state.error !== null,
},
actions: {
setData(data: any) {
this.data = data
},
async fetchData() {
this.loading = true
this.error = null
try {
const { data } = await useRequest(Apis.xxx.getData())
this.data = data.value
} catch (err) {
this.error = err as Error
} finally {
this.loading = false
}
},
reset() {
this.$reset()
},
},
})
带持久化的 Store
import { defineStore } from 'pinia'
import { createPersistedState } from '@/store/persist'
export const use{ModuleName}Store = defineStore('{moduleName}', {
state: () => ({
token: '',
userInfo: null,
}),
actions: {
setToken(token: string) {
this.token = token
},
},
persist: createPersistedState({
key: '{moduleName}-storage',
paths: ['token', 'userInfo'],
}),
})
类型定义
export interface {ModuleName}State {
data: {DataType} | null
loading: boolean
error: Error | null
}
export interface {DataType} {
id: string
name: string
}
使用 Store
在组件中使用
<script setup lang="ts">
const {moduleName}Store = use{ModuleName}Store()
// 访问状态
const data = computed(() => {moduleName}Store.data)
const loading = computed(() => {moduleName}Store.loading)
// 调用 action
onMounted(() => {
{moduleName}Store.fetchData()
})
</script>
在 composable 中使用
export function use{ModuleName}() {
const store = use{ModuleName}Store()
const doSomething = async () => {
await store.fetchData()
}
return {
data: computed(() => store.data),
loading: computed(() => store.loading),
doSomething,
}
}
持久化配置
export function createPersistedState(options: {
key: string
paths?: string[]
}) {
return {
key: options.key,
paths: options.paths,
storage: {
getItem: (key: string) => uni.getStorageSync(key),
setItem: (key: string, value: string) => uni.setStorageSync(key, value),
removeItem: (key: string) => uni.removeStorageSync(key),
},
}
}
注意事项
- Store 文件名使用
{moduleName}Store.ts 格式
- 导出的函数使用
use{ModuleName}Store 命名
- 复杂逻辑建议封装到 composable 中
- 敏感数据不要持久化到本地存储