Skip to content
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