by Sai gowtham

Vue Router Dynamic route matching tutorial

In this tutorial, we are going to learn about how to implement dynamic routing in vue router.

In vue router, we are rendering the components based on the path matching but sometimes if we have 10 users where we need to show the data of each user based on their id in such cases we need to use the dynamic segment in the path.

Dynamic segment is added by using colon :nameofthesegment

main.js
import Vue from 'vue'
import App from './App.vue';
import VueRouter from "vue-router";
import Home from './components/Home.vue';
import User from './components/User.vue';

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  routes: [
    { path: '/', component: Home },
     // dynamic segement `:id` is added to the path    { path: '/user/:id', component: User },  ]
})

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

Now inside our User component, we can access the dynamic segment with this.$route.params.

User.vue
<template>
  <div class="hello">
    <h1>User with id {{userId}}</h1>  </div>
</template>

<script>
export default {
  data: function() {
    return {
        //id is name of the dynamic segment we created in router
      userId: this.$route.params.id    };
  }
};
</script>

Now we can use the dynamic paths user/1 or user/2.

vue-url-params.png

Accessing Dynamic segement with props

We can also access the dynamic segment inside our component by using props.

To use props first we need to enable the props in our dynamic path.

main.js
import Vue from 'vue'
import App from './App.vue';
import VueRouter from "vue-router";
import Home from './components/Home.vue';
import User from './components/User.vue';

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  routes: [
    { path: '/', component: Home },
    //props are enabled    { path: '/user/:id', component: User, props: true },  ]
})

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

Now,in our User component we need to declare an id prop.

User.vue
<template>
  <div class="hello">
    <h1>User with id {{id}}</h1>  </div>
</template>

<script>
export default {
  props: ["id"]};
</script>

vue-route-url-props

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course