From dfb3b49d9e9fd03988e8884e737421b431281293 Mon Sep 17 00:00:00 2001 From: alexpts Date: Sat, 8 Jan 2022 17:54:23 +0300 Subject: [PATCH] Added option `isWatchState` for optional watch vuex --- src/index.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index d6d7ff2..21410cc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import { Router, RouteLocationNormalized } from 'vue-router' export interface SyncOptions { moduleName: string + isWatchState?: boolean } export interface State @@ -20,7 +21,10 @@ export function sync( router: Router, options?: SyncOptions ): () => void { - const moduleName = (options || {}).moduleName || 'route' + const {moduleName, isWatchState} = { + moduleName: 'route', + isWatchState: true + , ...options} store.registerModule(moduleName, { namespaced: true, @@ -36,7 +40,7 @@ export function sync( let currentPath: string // sync router on store change - const storeUnwatch = store.watch( + const storeUnwatch = isWatchState && store.watch( (state) => state[moduleName], (route: RouteLocationNormalized) => { const { fullPath } = route @@ -67,7 +71,9 @@ export function sync( afterEachUnHook() // remove store watch - storeUnwatch() + if (storeUnwatch) { + storeUnwatch() + } // unregister Module with store store.unregisterModule(moduleName)