Skip to content

Added option isWatchState #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 6.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Router, RouteLocationNormalized } from 'vue-router'

export interface SyncOptions {
moduleName: string
isWatchState?: boolean
}

export interface State
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -67,7 +71,9 @@ export function sync(
afterEachUnHook()

// remove store watch
storeUnwatch()
if (storeUnwatch) {
storeUnwatch()
}

// unregister Module with store
store.unregisterModule(moduleName)
Expand Down