Vue Router Tutorial With Example - How To Use Routing in Vuejs
Vue Router Tutorial With Example - How To Use Routing in Vuejs
In the JavaScript web application, the router is a part that syncs the currently
displayed view with the browser address bar content. In other words, routing is the
part that makes a URL change when you click something on a page and helps to
show the correct view or HTML when you navigate the specific URL.
If you want to learn more about Vue.js then check out this Vue JS 2 – The Complete
Guide (incl. Vue Router & Vuex)
Vue Router Tutorial With Example
installed Vue CLI 3.0, and if you have not installed, then you can install by typing
the following command.
sudo npm install -g @vue/cli
The above command needs to be executed on Administrator mode.
cd vueroute
I have installed the version 3.0.2 because, at this time, this is the latest version. You
might be getting a different version from the future.
Now, you can import the router inside the Vue.js application. So let us import inside
the src >> main.js file.
// main.js
Vue.use(VueRouter)
// routes.js
In the future, we will define the routes inside this file. Now, we need to import
this routes.js file inside the main.js file.
// main.js
Vue.config.productionTip = false;
Vue.use(VueRouter);
new Vue({
render: h => h(App),
routes
}).$mount('#app');
So, we have passed the router object while creating a vue instance.
// Home.vue
<template>
<div>
Home
</div>
</template>
<script>
export default {
}
</script>
// Register.vue
<template>
<div>
Register
</div>
</template>
<script>
export default {
}
</script>
// Login.vue
<template>
<div>
Login
</div>
</template>
<script>
export default {
}
</script>
Now, import all the components inside src >> routes.js file. We will define the
routes for each component.
// routes.js
const routes = [
{ path: '/', component: Home },
{ path: '/register', component: Register },
{ path: '/login', component: Login },
];
Next step is to create a VueRouter instance and pass the routes object. So, we need
to import routes.js file inside the main.js file.
// main.js
Vue.config.productionTip = false;
Vue.use(VueRouter);
new Vue({
router,
render: h => h(App)
}).$mount('#app');
Also, we need to display the routing components based on the routes. So we can do
that by adding the <router-view> component. So let us add that inside src >>
App.vue file.
// App.vue
<template>
<div id="app">
<nav>
<router-link to='/'>Home</router-link>
<router-link to='/register'>Register</router-link>
<router-link to='/login'>Login</router-link>
</nav>
<router-view />
</div>
</template>
<script>
export default {
}
</script>
Here, we have created basic navigation in which we have used the <router-link> to
change the different component via navigation.
Save this file and go to the terminal and start the vue development server.
We have used the Hash routing which is popular in building a Single Page
Application(SPA).
We need to modify one line of code inside the main.js file, and we are done.
// main.js
const router = new VueRouter({mode: 'history', routes});
We can achieve the dynamic segment. The above are were static segments.
Let us define one more route inside the src >> routes.js called /student/:id and also
create a component inside the src >> components folder called Student.vue.
// Student.vue
<template>
<div>
Student
</div>
</template>
<script>
export default {
}
</script>
Now, import the component inside the routes.js file and register that component.
// main.js
import Home from './components/Home.vue';
import Register from './components/Register.vue';
import Login from './components/Login.vue';
import Student from './components/Student.vue';
const routes = [
{ path: '/', component: Home },
{ path: '/register', component: Register },
{ path: '/login', component: Login },
{ path: '/student/:id', component: Student },
];
Here, in the above code, we have pass one parameter called id and that id is
different for every student.
Now inside the student route component, we can reference the route using the
$route and the access the id using $route.params.id. So, write the following code
inside the Student.vue file.
// Student.vue
<template>
<div>
Student ID is: {{ $route.params.id }}
</div>
</template>
<script>
export default {
}
</script>
this.$router.push()
this.$router.replace()
this.$router.go()
If you have performed the CRUD operations previously using Vue.js as a frontend
then we have used the push() method to change the route programmatically.
Sometimes it is more convenient to identify the route with the name, especially
when linking to the route or performing navigations. You can give the route a name
in the routes options while creating a Router instance. Now, we will add one
property called name to the routes object in a routes.js file.
// routes.js
const routes = [
{ path: '/', component: Home, name: 'home' },
{ path: '/register', component: Register, name: 'register' },
{ path: '/login', component: Login, name: 'login' },
{ path: '/student/:id', component: Student, name: 'student' },
];
// App.vue
<template>
<div id="app">
<nav>
<ul>
<li>
<router-link :to="{name: 'home'}">Home</router-link>
</li>
<li>
<router-link :to="{name: 'register'}">Register</router-link>
</li>
<li>
<router-link :to="{name: 'login'}">Login</router-link>
</li>
<li>
<router-link :to="{name: 'student', params: {id: 2}}">Student</router-
link>
</li>
</ul>
</nav>
<router-view />
</div>
</template>
<script>
export default {
}
</script>
// Redirect.vue
<template>
<div></div>
</template>
<script>
export default {
mounted() {
this.$router.push('/home');
}
}
</script>
So, when the component is mounted, we will redirect the page to the Home
component.
// main.js
const routes = [
{ path: '/', component: Home, name: 'home' },
{ path: '/register', component: Register, name: 'register' },
{ path: '/login', component: Login, name: 'login' },
{ path: '/student/:id', component: Student, name: 'student' },
{ path: '/redirect', component: Redirect, name: 'redirect' },
];
// 404.vue
<template>
<div>
Whoops 404!! The page is not available.
<a href="#" @click.prevent="back">Go back</a>
</div>
</template>
<script>
export default {
methods: {
back() {
this.$router.go(-1);
}
}
}
</script>
So, here, we have defined the 404 page and then put the link that will redirect to
the previous route. Here, Inside the function, I have passed the -1 which means
browser’s one step backward page from the browser and if we pass 1 then that
means one page forward inside the browser.
// routes.js
const routes = [
{ path: '/', component: Home, name: 'home' },
{ path: '/register', component: Register, name: 'register' },
{ path: '/login', component: Login, name: 'login' },
{ path: '/student/:id', component: Student, name: 'student' },
{ path: '/redirect', component: Redirect, name: 'redirect' },
{ path: '/404', component: Error, name: '404' },
];
Now, go to the http://localhost:8080/404, and you can see the link of the previous
page. Click that link, and you will go to the last page. So it is working and
programmatically.
Finally, Vue Router Tutorial With Example | How To Use Routing in VueJS is over.