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 637bf30
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
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
20 changes: 13 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,14 @@
import { reactive } from 'vue';
import { createPinia, defineStore } from 'pinia';

export const themeStore = reactive({
theme: 'light',
toggle() {
this.theme = this.theme === 'light' ? 'dark' : 'light';
},
});
/** 这是全局实例,不需要使用 App.use() 注入 */
const GLOBAL_REF = createPinia();

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

0 comments on commit 637bf30

Please sign in to comment.