forked from antfu-collective/vitesse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pinia stores (antfu-collective#170)
- Loading branch information
Showing
11 changed files
with
96 additions
and
88 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
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 |
---|---|---|
|
@@ -9,5 +9,6 @@ intro: | |
desc: 固执己见的 Vite 项目模板 | ||
dynamic-route: 动态路由演示 | ||
hi: 你好,{name} | ||
aka: 也叫 | ||
whats-your-name: 输入你的名字 | ||
not-found: 未找到页面 |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
import { createPinia } from 'pinia' | ||
import { UserModule } from '~/types' | ||
|
||
export const install: UserModule = ({ isClient, initialState, app }) => { | ||
const pinia = createPinia() | ||
app.use(pinia) | ||
// Refer to | ||
// https://github.com/antfu/vite-ssg/blob/main/README.md#state-serialization | ||
// for other serialization strategies. | ||
if (isClient) | ||
pinia.state.value = (initialState.pinia) || {} | ||
|
||
else | ||
initialState.pinia = pinia.state.value | ||
} |
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,34 @@ | ||
import { acceptHMRUpdate, defineStore } from 'pinia' | ||
|
||
export const useUserStore = defineStore('user', () => { | ||
/** | ||
* Current named of the user. | ||
*/ | ||
const savedName = ref('') | ||
const previousNames = ref(new Set<string>()) | ||
|
||
const usedNames = computed(() => Array.from(previousNames.value)) | ||
const otherNames = computed(() => usedNames.value.filter(name => name !== savedName.value)) | ||
|
||
/** | ||
* Changes the current name of the user and saves the one that was used | ||
* before. | ||
* | ||
* @param name - new name to set | ||
*/ | ||
function setNewName(name: string) { | ||
if (savedName.value) | ||
previousNames.value.add(savedName.value) | ||
|
||
savedName.value = name | ||
} | ||
|
||
return { | ||
setNewName, | ||
otherNames, | ||
savedName, | ||
} | ||
}) | ||
|
||
if (import.meta.hot) | ||
import.meta.hot.accept(acceptHMRUpdate(useUserStore, import.meta.hot)) |