From 43d824f52b7479a72752aa463f925b2c46792a18 Mon Sep 17 00:00:00 2001 From: rca Date: Thu, 18 Jul 2024 23:40:34 +0900 Subject: [PATCH] wip --- src/store/account/actions.ts | 4 +- src/store/account/getters.ts | 3 +- src/store/account/index.ts | 4 +- src/store/index.ts | 90 +++++++++++++++++------------------- 4 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/store/account/actions.ts b/src/store/account/actions.ts index 27bb88f..2b4cc24 100644 --- a/src/store/account/actions.ts +++ b/src/store/account/actions.ts @@ -1,8 +1,8 @@ import { ActionTree } from 'vuex'; -import { StateInterface } from '../index'; +import { RootStateInterface } from '../index'; import { AccountStateInterface } from './state'; -const actions: ActionTree = { +const actions: ActionTree = { logout(context) { context.commit('setToken', ''); context.commit('setUsername', ''); diff --git a/src/store/account/getters.ts b/src/store/account/getters.ts index a5d370e..343ec3e 100644 --- a/src/store/account/getters.ts +++ b/src/store/account/getters.ts @@ -1,8 +1,7 @@ import { GetterTree } from 'vuex'; -import { StateInterface } from '../index'; import { AccountStateInterface } from './state'; -const getters: GetterTree = { +const getters: GetterTree = { getToken(state: AccountStateInterface): string { return state.token; }, diff --git a/src/store/account/index.ts b/src/store/account/index.ts index 7e58645..c4ca657 100644 --- a/src/store/account/index.ts +++ b/src/store/account/index.ts @@ -1,11 +1,11 @@ import { Module } from 'vuex'; -import { StateInterface } from '../index'; +import { RootStateInterface } from '../index'; import state, { AccountStateInterface } from './state'; import actions from './actions'; import getters from './getters'; import mutations from './mutations'; -const accountStateModule: Module = { +const accountStateModule: Module = { namespaced: true, actions, getters, diff --git a/src/store/index.ts b/src/store/index.ts index 5e3e1ec..8d402b2 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,63 +1,59 @@ -import { store } from 'quasar/wrappers' -import { InjectionKey } from 'vue' -import { Router } from 'vue-router' +import { store } from 'quasar/wrappers'; import { createStore, - Store as VuexStore, - useStore as vuexUseStore, -} from 'vuex' + Module, + createComposable, + Getters, + Mutations, +} from 'vuex-smart-module'; +import { AccountStateInterface } from './account/state'; -// import example from './module-example' -// import { ExampleStateInterface } from './module-example/state'; - -/* - * If not building with SSR mode, you can - * directly export the Store instantiation; - * - * The function below can be async too; either use - * async/await or return a Promise which resolves - * with the Store instance. - */ - -export interface StateInterface { - // Define your own store structure, using submodules if needed - // example: ExampleStateInterface; - // Declared as unknown to avoid linting issue. Best to strongly type as per the line above. - example: unknown +class RootState { + count = 1; } -// provide typings for `this.$store` -declare module '@vue/runtime-core' { - interface ComponentCustomProperties { - $store: VuexStore +class RootGetters extends Getters { + get count () { + return this.state.count; + } + + multiply (multiplier: number) { + return this.state.count * multiplier; } } -// provide typings for `useStore` helper -export const storeKey: InjectionKey> = Symbol('vuex-key') - -// Provide typings for `this.$router` inside Vuex stores -declare module "vuex" { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - export interface Store { - readonly $router: Router; +class RootMutations extends Mutations { + add (payload: number) { + this.state.count += payload; } } +// This is the config of the root module +// You can define a root state/getters/mutations/actions here +// Or do everything in separate modules +const rootConfig = { + state: RootState, + getters: RootGetters, + mutations: RootMutations, + modules: { + // + }, +}; + +export const root = new Module(rootConfig); + export default store(function (/* { ssrContext } */) { - const Store = createStore({ - modules: { - // example - }, + const rootStore = createStore(root, { + strict: !!process.env.DEBUGGING, + // plugins: [] + // and other options, normally passed to Vuex `createStore` + }); - // enable strict mode (adds overhead!) - // for dev mode and --debug builds only - strict: !!process.env.DEBUGGING - }) + return rootStore; +}); - return Store; -}) +export const useStore = createComposable(root); -export function useStore() { - return vuexUseStore(storeKey) +export interface RootStateInterface { + account: AccountStateInterface; }