Vuex Router Sync

5.0.0 · active · verified Wed Apr 22

The `vuex-router-sync` library provides a straightforward utility to keep `vue-router`'s current route state synchronized with a `vuex` store. Its core `sync` function injects the current route object into the Vuex state, typically at `store.state.route`, enabling reactive access to route parameters, query strings, and path information directly from the store. This simplifies component logic that depends on route data, reducing the need to pass `$route` explicitly. The current stable version, 5.0.0, is compatible with `vue-router >= 3.0.0` and `vuex >= 3.0.0` and includes comprehensive TypeScript definitions. A release candidate for v6.0.0 is under development, targeting compatibility with the Vue 3 ecosystem (Vuex 4, Vue Router 4). The package has a consistent release cadence, typically aligning with major version updates of Vue's core routing and state management libraries. It differentiates itself by offering a simple, drop-in solution for a common integration pattern.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic setup and usage of `vuex-router-sync` to expose the current router state via `store.state.route`.

import Vue from 'vue';
import Vuex from 'vuex';
import VueRouter from 'vue-router';
import { sync } from 'vuex-router-sync';

Vue.use(Vuex);
Vue.use(VueRouter);

const routes = [
  { path: '/', component: { template: '<div>Home Page</div>' } },
  { path: '/about', component: { template: '<div>About Page</div>' } },
  { path: '/users/:id', component: { template: '<div>User ID from store: {{ $store.state.route.params.id }}</div>' } }
];

const router = new VueRouter({
  routes
});

const store = new Vuex.Store({
  state: {
    appData: 'Initial Application Data'
  },
  getters: {
    currentRoutePath: state => state.route.path // Access route info from store
  }
});

// Synchronize Vue Router with Vuex store
sync(store, router);

new Vue({
  router,
  store,
  template: `
    <div id="app">
      <h1>Vuex Router Sync Example</h1>
      <p>Current path (from Vuex store): <strong>{{ $store.state.route.path }}</strong></p>
      <p>Current path (from Vue Router): <strong>{{ $route.path }}</strong></p>
      <nav>
        <router-link to="/">Home</router-link> |
        <router-link to="/about">About</router-link> |
        <router-link to="/users/42">User 42</router-link>
      </nav>
      <hr>
      <router-view></router-view>
    </div>
  `
}).$mount('#app');

view raw JSON →