← Back to Vue.js Course | Chapter 7: Vue Router | Lesson 1 of 7

Installing Vue Router

Vue Router is the official routing library that maps URLs to components.

In this page:

  1. Installing Vue Router
Syntax
markup
npm install vue-router@4

Installing Vue Router

In a project install it with npm install vue-router@4, or add it while scaffolding with create-vue. Create a router with createRouter and a history mode, then register it with app.use(router). From the CDN it is available as the VueRouter global.

Note: Choose createWebHistory for clean URLs or createWebHashHistory when you cannot configure the server.

Example: Installing Vue Router

bash
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
$ npm install vue-router@4

// main.js
import { createApp } from "vue";
import { createRouter, createWebHistory } from "vue-router";
import App from "./App.vue";

const router = createRouter({ history: createWebHistory(), routes: [] });
createApp(App).use(router).mount("#app");
</body>
</html>

⚠️ Run this in your own terminal or Node.js environment.

Live Example
Related Topics
Common Mistakes
  1. Installing vue-router 3 for Vue 3
  2. Forgetting app.use(router)
  3. Using web history without server fallback rules
Chapter Summary
  • npm install vue-router@4
  • createRouter builds the router
  • app.use(router) installs it
  • Pick a history mode
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.