feat(front): add Pinia for state management and create useStore composable for improved state handling

This commit is contained in:
keven1024
2025-04-13 22:16:32 +08:00
parent 4d59880365
commit 07efc20ea2
3 changed files with 37 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import { defineStore } from 'pinia'
import { cloneDeep, get, isEmpty, isUndefined, set,isString } from 'lodash-es'
type StoreType = Record<string, any>
const initState: StoreType = {}
// 做了一点小小的改进可以传入key会自动初始化如果不初始化的话容易导致不存在值而丢失响应式
const useStore = (key?: string) => {
const store = defineStore('store', {
state: () => ({
...initState,
}),
actions: {
_get(path?: string) {
if (isEmpty(path) || isUndefined(path)) {
return this.$state
}
return get(this.$state, path)
},
_set(path: string, value: any) {
const newState = cloneDeep(this.$state)
set(newState, path, value)
this.$patch(newState)
},
},
persist: true,
})()
if (!isEmpty(key) && isString(key) && isUndefined(store?._get(key))) {
console.log('reset', key, store?._get(key))
store?._set(key, undefined)
}
return store
}
export default useStore

View File

@@ -94,6 +94,7 @@ export default defineNuxtConfig({
"nuxt-lucide-icons",
"shadcn-nuxt",
'@vee-validate/nuxt',
'@pinia/nuxt',
],
serwist: {},
vite: {

View File

@@ -10,6 +10,7 @@
"postinstall": "nuxt prepare"
},
"dependencies": {
"@pinia/nuxt": "^0.11.0",
"@tailwindcss/postcss": "^4.1.3",
"@tailwindcss/vite": "^4.1.3",
"@tiptap/extension-blockquote": "^2.11.7",