Effortlessly keep vue-router and vuex store in sync.
npx @tessl/cli install tessl/npm-vuex-router-sync@5.0.0vuex-router-sync provides seamless synchronization between Vue Router and Vuex store state, enabling developers to access current route information directly from the Vuex store. It creates an immutable route module in the store that automatically updates when navigation occurs, eliminating the need for manual route state management.
npm install vuex-router-syncimport { sync } from 'vuex-router-sync';For TypeScript:
import { sync, type SyncOptions } from 'vuex-router-sync';For CommonJS:
const { sync } = require('vuex-router-sync');import { sync } from 'vuex-router-sync';
import store from './vuex/store'; // vuex store instance
import router from './router'; // vue-router instance
// Sync router and store - returns unsync callback
const unsync = sync(store, router);
// Route information is now available in store
console.log(store.state.route.path); // current path
console.log(store.state.route.params); // current params
console.log(store.state.route.query); // current query
// Later, during app teardown
unsync(); // Clean up synchronizationvuex-router-sync works by:
route module in the Vuex store containing current route stateThe synchronized route state includes:
path - Current route pathparams - Route parameters objectquery - Query parameters objectfullPath - Complete path including query and hashhash - URL hashname - Route namemeta - Route metadatafrom - Previous route (during navigation)Creates synchronization between Vue Router and Vuex store.
/**
* Synchronizes Vue Router and Vuex store state
* @param {Object} store - Vuex store instance
* @param {Object} router - Vue Router instance
* @param {SyncOptions} [options] - Configuration options
* @returns {() => void} Unsync callback function for cleanup
*/
function sync(store, router, options);/**
* TypeScript declaration for sync function
*/
function sync(
store: Store<any>,
router: VueRouter,
options?: SyncOptions
): () => void;Usage Examples:
import { sync } from 'vuex-router-sync';
// Basic synchronization with default module name 'route'
const unsync = sync(store, router);
// Custom module name
const unsync = sync(store, router, { moduleName: 'router' });
// Access route data from store
store.state.route.path; // "/users/123"
store.state.route.params; // { id: "123" }
store.state.route.query; // { tab: "profile" }
store.state.route.fullPath; // "/users/123?tab=profile#settings"import { Store } from 'vuex';
import VueRouter from 'vue-router';
interface SyncOptions {
/** Name of the module to register in Vuex store (defaults to 'route') */
moduleName?: string;
}
interface RouteState {
/** Current route name */
name: string | null;
/** Current route path */
path: string;
/** URL hash */
hash: string;
/** Query parameters object */
query: Record<string, string | (string | null)[]>;
/** Route parameters object */
params: Record<string, string>;
/** Full path including query and hash */
fullPath: string;
/** Route metadata */
meta: any;
/** Previous route (when navigating) */
from?: RouteState;
}The sync function assumes valid Vuex store and Vue Router instances. No specific errors are thrown, but improper usage may lead to:
vue-router ^3.0.0vuex ^3.0.0