vuejs
/

Vue Router Introduction

Last Sync: Today

On this page

11
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

vuejs

Vue Router Introduction

What is Vue Router?

Vue Router is the official routing library for Vue.js used to create navigation between different views in a single-page application (SPA).

Why Use Vue Router?

  • Enable SPA navigation
  • Manage routes easily
  • Dynamic component rendering
  • Better user experience without page reload

Basic Concept

Instead of loading new pages, Vue Router dynamically updates components based on the URL.

Install Vue Router

BASHRead-only
1
npm install vue-router

Basic Setup

JavaScriptRead-only
1
import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

Router View

HTMLRead-only
1
<router-view></router-view>

Navigation Links

HTMLRead-only
1
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>

Programmatic Navigation

JavaScriptRead-only
1
this.$router.push('/about');

Best Practices

  • Use named routes for clarity
  • Organize routes in separate files
  • Use lazy loading for routes
  • Keep routing logic clean

Common Mistakes

  • Not defining routes properly
  • Forgetting router-view
  • Using wrong paths
  • Not handling navigation errors

Conclusion

Vue Router is essential for building modern SPAs, enabling smooth navigation and dynamic component rendering.

Try it yourself

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

Test Your Knowledge

Q1
of 3

Router used for?

A
API
B
Navigation
C
Database
D
Styling
Q2
of 3

Which renders component?

A
router-link
B
router-view
C
router-app
D
router-div
Q3
of 3

Install command?

A
npm install vue
B
npm install router
C
npm install vue-router
D
npm router

Frequently Asked Questions

What is Vue Router?

A library for handling navigation in Vue apps.

What is router-view?

It renders matched components.

What is router-link?

It creates navigation links.

Previous

vue lifecycle hooks

Next

vue dynamic routing

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.