Today I Learned

How to add dynamic routes with VUE router

Dynamic routes are an important part. Let's consider we have a blog (for example) which has 10 posts, each of them with a unique URL. Creating a new component for each blog post isn't the best practice, so here intervenes a special feature of VUE: dynamic routes.

First, let's create a Post component which will serve as a template for displaying each unique blog post:

		<h1>Welcome to Post page</h1>
		<p>This is the page for post number {{ $route.params.id }}</p>

Now, we need to update the router/index.js with our new route. *Don't forget to include the import for the Post component.

Below, we will use :id parameter as a dynamic value in the route:

	path: '/post/:id',
	name: 'Post',
	component: Post

Nicely done, you have now dynamic routes for your blog.