techferment
Published in

techferment

8. The Vue Router

Installation

We can install the Vue router into an existing Vue.js project by running the following command in the terminal

npm install vue-router
import Vue from 'vue'
import router from './router'

Vue.use(router)

Usage

The usage of the vue-router is very simple and easy to use. First, in the template or HTML

// App.vue<template>
// Navbar
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view />
</template>
import Comp from "../views/Contact.vue";
const routes = [
{
path: "/contact",
name: "Contact",
component: Contact
}
];
const routes = [
{
path: "/contact",
name: "Contact",
component: Contact
},
{
path: "/about",
name: "About",
component: About
}
];
const router = createRouter({
routes // short for `routes: routes`
});
createApp(App)
.use(router)
.mount("#app");
methods: {
clickFunc() {
this.$router.push('/about')
}
}

--

--

Our mission is to equip our community members with practical skills, enabling them to communicate their insights and drive innovative solutions effectively. We are a thriving engineering community, dedicated to fostering a network of inquisitive individuals.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vrijraj Singh

Google Developer Expert for Web Technologies & Firebase | Developer Advocate | GDG Organizer for GDG Jalandhar