Creating a link in Vue

Any HTML is legal, so you can just write a regular link, but if you want to have a fast response, it is recommended that you include Vue Router. This brings two important tags: <router-link>, which this post is mainly about and <router-view>.

Creating a link in Vue is as simple as:

<router-link to="home">Home</router-link>

This renders a perfectly correct link, using the earlier mentioned a-tag. It even adds a nice bonus, adding an ‘active’ class when that route is active. If you prefer this class on the containing element, do this:

<router-link to="home" tag="li"><a>Home</a></router-link>

This will render something along the lines of:

<li><a href="home">Home</a></li>

Be aware: this is the only valid use-case for the tag-property in vue’s router-link component. Hence be wary if you come across any use of the tag-property, but Vue’s default is good.

You pick up on this link by defining the route ‘home’, make sure your app contains a <router-view> and attach a component to this route. In the most basic form:

<div id="app">
  <nav role="menu">
    <router-link to="/">Home</router-link>
    <router-link to="/about">About</router-link>

The JS:

const Home = {template: '<div><h1>Home</h1></div>'}
const About = {template: '<div>h1>About</h1></div>'}

const router = new VueRouter({
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About }

const app = new Vue({

See here for a minimal vue Router Example on CodePen.

That’s it. If you enjoy my sensible approach to front-end web development, don’t forget to subscribe below!

How do I navigate to another page in React or Vue?

Congratulations, you started working with one of the popular front-end frameworks. Both Vue and React are excellent choices to create rich and reactive applications. But sometimes, you ‘just’ want to show the user another page. Whether it is a simple about page, or another ‘section’ in the app. In this post I will cover both Vue en React and, little bonus, “React on steroids”, NextJS.

It is important to know that you need a router to make in-app pagination work. If you look up router and your favourite framework you’re probably find a way to do it, accompanied with some ways to actually not do it. Hence, here I’ll focus on the right way.

The basics of every link

While you can make everything clickable (or touch-able), it is important that enabling navigation between pages was already a core concept when the web was invented. Even when writing webpages using React, Vue, or most other JavaScript frameworks, it ends up as HTML to the browser (with JavaScript & CSS). HTML stands for Hyper Text Markup  Language and HyperText is text with hyperlinks. Hyperlinks in HTML are written as follows:

<a href="">Example link</a>

So why is this important? On parsing code presented as above, the browser does the following:

  • It colours the links (typically) blue & underlines them (styles them as links).
  • On mouse-over you get a little hand-pointer
  • When using the keyboard you can ‘tab’-to the link.
  • Text-to-speech engines can announce it as being a link
  • Search engines can follow the link (may, but not always, require server-side rendering)

While the code above looks easy, it is easy with modern frameworks to create something like this:

<div onclick="open('')">Example link</div>

This is clickable, and yes, it might navigate you to, but shares none of the desirable characteristics of the well known hyperlink.

The boring fact

The boring fact is that the above code, yes with the <a href=..., works in every framework. In the next few posts I’ll dive into the ways to make it  work more natively in the frameworks mentioned earlier, often resulting in much faster page loads when you stay within your React/Vue or whatever application. Also, I’ll give a bit of a review of the good parts of these and make sure you don’t use the bad parts (if any). To stay up to date, make sure you subscribe to my mailinglist!