mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
fix: update useStore to set key value to null instead of undefined for better state management
This commit is contained in:
@@ -1,35 +1,34 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { cloneDeep, get, isEmpty, isUndefined, set,isString } from 'lodash-es'
|
||||
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
|
||||
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)
|
||||
},
|
||||
},
|
||||
})()
|
||||
if (!isEmpty(key) && isString(key) && isUndefined(store?._get(key))) {
|
||||
// console.log('reset', key, store?._get(key))
|
||||
store?._set(key, null)
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
export default useStore
|
||||
|
||||
Reference in New Issue
Block a user