How to install vue-router in Vue project

Vue Router is the official router for Vue.js.

To install Vue Router type this in command prompt.

npm install vue-router@4

After installing Vue Router, enter the script in main.js file

import { router } from "./router/router.js";
app.use(router);

Given is the Javascript sample of Vue Router

import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
import component1 from "../View/component1.vue"
import component2 from "../View/component2.vue"
export const router = createRouter({
    history: createWebHistory(),
    routes: [{
        path: "/component1",
        name: "component1",
        component: component1,
    },
    {
        path: "/component2",
        name: 'component2',
        component: component2
    }
    ]
});

Leave a comment

Your email address will not be published. Required fields are marked *