From a6544d2a7c790d5e6676b48e11d276f35e615235 Mon Sep 17 00:00:00 2001 From: drodichkin Date: Thu, 24 Feb 2022 19:55:46 +0300 Subject: [PATCH] For #266: add docs for new methods, add unregisterModule Nuxt support --- docs/pages/en/2.accessor/2.dynamic-modules.md | 86 +++++++++++++++++-- .../src/types/accessorRegisterModule.ts | 5 ++ packages/nuxt-typed-vuex/template/plugin.js | 5 +- 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/docs/pages/en/2.accessor/2.dynamic-modules.md b/docs/pages/en/2.accessor/2.dynamic-modules.md index 132efad..95dbfc1 100644 --- a/docs/pages/en/2.accessor/2.dynamic-modules.md +++ b/docs/pages/en/2.accessor/2.dynamic-modules.md @@ -1,11 +1,12 @@ --- -title: Dynamic modules -description: 'Vanilla, strongly-typed store accessor.' +title: Dynamic modules description: 'Vanilla, strongly-typed store accessor.' --- You can also use `typed-vuex` with dynamic modules. -## Sample module +## Variable usage + +### Sample module ```ts{}[modules/dynamic-module.ts] export const namespaced = true @@ -21,7 +22,7 @@ export const mutations = mutationTree(state, { }) ``` -## Accessing the module +### Accessing the module You might want to use the store @@ -29,7 +30,7 @@ You might want to use the store import Vue from 'vue import { useAccessor, getAccessorType } from 'typed-vuex' -import dynamicModule from '~/modules/dynamic-module' +import * as dynamicModule from '~/modules/dynamic-module' const accessorType = getAccessorType(dynamicModule) @@ -59,5 +60,80 @@ export default Vue.extend({ } } }) +``` + +## Global usage + +You can also register dynamic module on global level for your accessor. This can be a risky way, but can also be a way +for large projects with no able of using `useAccessor` as variable. + +### Module definition + +```ts{}[store/index.ts] +import * as myImportedModule from '~/modules/myImportedModule' +import * as myNestedModule from '~/modules/myNestedModule' + +export const accessorType = getAccessorType({ + state, + getters, + mutations, + actions, + modules: { + submodule: { + //add this for module to show as possibly-undefined when using $accessor + dynamic: true, + ...myImportedModule, + modules: { + myNestedModule, + }, + }, + }, +}) +``` + +### Modules registration + +```ts{}[components/my-component.vue] +import Vue from 'vue +//Import from typed-vuex is not required in Nuxt +import { registerModule, unregisterModule } from 'typed-vuex' +import myImportedModule from '~/modules/myImportedModule' +import myNestedModule from '~/modules/myNestedModule' + +export default Vue.extend({ + beforeCreated() { + //Or this.$accessorRegisterModule('myImportedModule', myImportedModule) in Nuxt + registerModule('myImportedModule', this.$store, this.$accessor, myImportedModule) + + //Or this.$accessorRegisterModule(['myImportedModule', 'myNestedModule'], myImportedModule) in Nuxt + registerModule(['myImportedModule', 'myNestedModule'], this.$store, this.$accessor, myNestedModule) + + //You can also register two modules at once, but you MUST include namespaced: true in that case + //this.$accessorRegisterModule('myImportedModule', {...}) syntax in Nuxt + registerModule('myImportedModule', { + ...myImportedModule, + modules: { + myNestedModule: { + //required for correct usage, especially in Nuxt + namespaced: true, + ...myNestedModule, + }, + }, + }) + + //To unregister module(s), simply call this + //this.$accessorUnregisterModule('myImportedModule') in Nuxt + unregisterModule('myImportedModule', this.$store, this.$accessor) + }, + methods: { + anotherMethod() { + //If you are not sure about if module will always exist, + // it is recommended to enable dynamic: true in store and always check for undefined + if (this.accessor) { + this.$accessor.myImportedModule?.myNestedModule?.addEmail('my@email.com') + } + } + } +}) ``` diff --git a/packages/nuxt-typed-vuex/src/types/accessorRegisterModule.ts b/packages/nuxt-typed-vuex/src/types/accessorRegisterModule.ts index 4dd3292..f0884dc 100644 --- a/packages/nuxt-typed-vuex/src/types/accessorRegisterModule.ts +++ b/packages/nuxt-typed-vuex/src/types/accessorRegisterModule.ts @@ -1,20 +1,24 @@ import type {Module} from "vuex"; type AccessorRegisterModule = (path: string | [string, ...string[]], module: Module) => void +type AccessorUnregisterModule = (path: string | [string, ...string[]]) => void declare module '@nuxt/types' { interface NuxtAppOptions { $accessorRegisterModule: AccessorRegisterModule; + $accessorUnregisterModule: AccessorUnregisterModule; } interface Context { $accessorRegisterModule: AccessorRegisterModule; + $accessorUnregisterModule: AccessorUnregisterModule; } } declare module 'vue/types/vue' { interface Vue { $accessorRegisterModule: AccessorRegisterModule; + $accessorUnregisterModule: AccessorUnregisterModule; } } @@ -22,5 +26,6 @@ declare module 'vuex/types/index' { // eslint-disable-next-line @typescript-eslint/no-unused-vars interface Store { $accessorRegisterModule: AccessorRegisterModule; + $accessorUnregisterModule: AccessorUnregisterModule; } } diff --git a/packages/nuxt-typed-vuex/template/plugin.js b/packages/nuxt-typed-vuex/template/plugin.js index a8dcd6a..b6471a1 100644 --- a/packages/nuxt-typed-vuex/template/plugin.js +++ b/packages/nuxt-typed-vuex/template/plugin.js @@ -1,4 +1,4 @@ -import { getAccessorFromStore, registerModule } from 'typed-vuex' +import { getAccessorFromStore, registerModule, unregisterModule } from 'typed-vuex' import { createStore } from '<%= options.store %>' @@ -10,4 +10,7 @@ export default async ({ store }, inject) => { inject('accessorRegisterModule', (path, module) => { registerModule(path, store, accessor, module) }) + inject('accessorUnregisterModule', (path) => { + unregisterModule(path, store, accessor) + }) }