-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): add pinia setup syntax example: setup-store[添加setup s…
…yntax的pinia示例setup-store]
- Loading branch information
1 parent
a539112
commit 82c4b09
Showing
4 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { reactive } from 'vue'; | ||
import { defineStore } from 'pinia'; | ||
import { useBoolean } from '@/hooks'; | ||
|
||
export const useSetupStore = defineStore('setup-store', () => { | ||
const { bool: visible, setTrue: show, setFalse: hide } = useBoolean(); | ||
|
||
interface Config { | ||
name: string; | ||
} | ||
|
||
const config = reactive<Config>({ name: 'config' }); | ||
|
||
/** 设置配置 */ | ||
function setConfig(conf: Partial<Config>) { | ||
Object.assign(config, conf); | ||
} | ||
|
||
return { | ||
visible, | ||
show, | ||
hide, | ||
config, | ||
setConfig | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { PiniaPluginContext } from 'pinia'; | ||
import { cloneDeep } from 'lodash-es'; | ||
|
||
/** | ||
* setup语法的重置状态插件 | ||
* @param context | ||
* @description 请将用setup语法的状态id写入到setupSyntaxIds | ||
*/ | ||
export function resetSetupStore(context: PiniaPluginContext) { | ||
const setupSyntaxIds = ['setup-store']; | ||
|
||
if (setupSyntaxIds.includes(context.store.$id)) { | ||
const { $state } = context.store; | ||
|
||
const defaultStore = cloneDeep($state); | ||
|
||
context.store.$reset = () => { | ||
context.store.$patch(defaultStore); | ||
}; | ||
} | ||
} |