Skip to content

Commit

Permalink
chore: using pinia implement global store
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqours committed Dec 19, 2023
1 parent d6077da commit f3641d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
3 changes: 2 additions & 1 deletion apps/vue-demo/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import './style/global.less';
import RootView from './component/RootView.vue';
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import { globalPinia } from './store/global/theme.store';

export function createVueApp(container: HTMLElement, node?: HTMLElement) {
const app = createApp(RootView);
app.use(createPinia()).mount(container);
app.use(globalPinia).use(createPinia()).mount(container);

return () => app.unmount();
}
6 changes: 3 additions & 3 deletions apps/vue-demo/src/component/pinia/PiniaThemeSwitch.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<button @click="toggleTheme">Switch Theme</button>
<button @click="themeStore.toggle()">Switch Theme</button>
</template>

<script setup lang="ts">
import { themeStore } from '../../store/global/theme.store';
import { useThemeStore } from '../../store/global/theme.store';
import { watch } from 'vue';
const toggleTheme = () => themeStore.toggle();
const themeStore = useThemeStore();
watch(
() => themeStore.theme,
Expand Down
19 changes: 12 additions & 7 deletions apps/vue-demo/src/store/global/theme.store.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { reactive } from 'vue';
import { createPinia, defineStore } from 'pinia';

export const themeStore = reactive({
theme: 'light',
toggle() {
this.theme = this.theme === 'light' ? 'dark' : 'light';
},
});
export const globalPinia = createPinia();

export const useThemeStore = () =>
defineStore('theme', {
state: () => ({ theme: 'light' }),
actions: {
toggle() {
this.theme = this.theme === 'light' ? 'dark' : 'light';
},
},
})(globalPinia);

0 comments on commit f3641d1

Please sign in to comment.