Categories
Vue Answers

How to get Vue Router router parameters into Vuex actions?

Spread the love

Sometimes, we want to get Vue Router router parameters into Vuex actions.

In this article, we’ll look at how to get Vue Router router parameters into Vuex actions.

How to get Vue Router router parameters into Vuex actions?

To get Vue Router router parameters into Vuex actions, we can import the router directly and use router.currentRoute.params to access the route URL parameter values.

For instance, we write

router.js

import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./routes";

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  routes,
});

export default router;

to create our router and export it.

Then we write

import router from "@/router";

//...
router.currentRoute.params.id;
//...

to import the router and then use router.currentRoute.params.id to get the value of the id route URL parameter.

Conclusion

To get Vue Router router parameters into Vuex actions, we can import the router directly and use router.currentRoute.params to access the route URL parameter values.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *