pinia-store-generator
3
总安装量
3
周安装量
#59074
全站排名
安装命令
npx skills add https://github.com/wot-ui/wot-starter --skill pinia-store-generator
Agent 安装分布
gemini-cli
3
claude-code
3
amp
3
github-copilot
3
openclaw
3
codex
3
Skill 文档
Pinia Store çæå¨
å¿«éå建符å wot-starter 项ç®è§èç Pinia Storeã
ç®å½ç»æ
src/store/
âââ persist.ts # æä¹
åé
ç½®
âââ themeStore.ts # ä¸»é¢ store 示ä¾
âââ manualThemeStore.ts # æå¨ä¸»é¢ store 示ä¾
âââ {moduleName}Store.ts # æ°å»ºç store
Store 模æ¿
Options Storeï¼æ¨èï¼
// src/store/{moduleName}Store.ts
import type { {ModuleName}State } from '@/types/{moduleName}'
import { defineStore } from 'pinia'
/**
* {moduleName} ç¶æç®¡ç
* æè¿°è¿ä¸ª store çç¨é
*/
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: {
// 忥 action
setData(data: any) {
this.data = data
},
// 弿¥ action
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
// src/store/{moduleName}Store.ts
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'], // åªæä¹
åæå®å段
}),
})
ç±»åå®ä¹
// src/types/{moduleName}.ts
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 ä¸ä½¿ç¨
// src/composables/use{ModuleName}.ts
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,
}
}
æä¹ åé ç½®
// src/store/persist.ts
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 ä¸
- æææ°æ®ä¸è¦æä¹ åå°æ¬å°åå¨