Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 67

Vue Router: Navigation & Views

Recommended resources

Hello! In our last lesson, you did a great job learning how to create dynamic routes, like /projects/:id. You now know how to define a route that can match many different URLs and how to access the dynamic id from that URL within your component, even passing it as a prop for cleaner code.

However, defining routes is only one part of the puzzle. How do we actually render the components for those routes, and how do users navigate between them without reloading the entire application?

This lesson focuses on precisely that. Your goal is to use <RouterLink> and <RouterView> to implement navigation and render page components. These two special components are the backbone of the user-facing side of Vue Router. You'll learn how to structure your main App.vue file to host a persistent navigation bar and a content area where your page components will be displayed dynamically.


The Big Picture: How Routing Components Work

Before we dive into the code, let's look at a high-level diagram. This will help you visualize how the pieces fit together in a single-page application (SPA).

Vue 3 Application Architecture with Routing and Data Flow
This diagram shows the architecture of a Vue application with routing. The URL determines which component the `<RouterView>` renders. Notice how the router acts as a controller, swapping out components like `TutorialsList` or `AddTutorial` based on the path, while those components might then fetch their own data.

As you can see, RouterView is the central "outlet" where different page components appear. The navigation links, which we'll build with RouterLink, tell the router to change the component displayed in that outlet.


Part 1: <RouterView> - The Content Outlet

The <RouterView> component is a placeholder that marks the spot where the component for the current route should be rendered.

Think of your App.vue file as the frame of your application. It might contain a consistent header, navigation bar, and footer that should appear on every page. The <RouterView> is the canvas within that frame, and Vue Router paints the appropriate page component onto it based on the URL.

A typical structure for your main App.vue file would look like this:

<template>
  <header>
    <!-- Your persistent navigation will go here -->
  </header>

  <main>
    <!-- This is where your page components will be rendered -->
    <RouterView />
  </main>

  <footer>
    <!-- A persistent footer -->
  </footer>
</template>

To see a clear explanation of how <RouterView> works and where to place it, watch the following video segment.

Vue 3 Routing - Beginner to Advanced

The 'Laiture' channel provides a very clear, modern explanation using the Composition API. This clip will show you exactly what <RouterView> does and why it's essential.

Watch from 21:52 to 25:57. The key takeaway is understanding that <RouterView> is a special component that dynamically renders your HomeView, AboutView, etc., based on the current URL path.



Part 2: <RouterLink> - Navigating Without Page Reloads

Now that we have a place to render our components, we need a way to navigate between them. You might be tempted to use a standard HTML anchor tag (<a>). However, in an SPA, this is the wrong tool for the job.

An <a> tag with an href attribute will trigger a full-page reload from the server. This re-initializes your entire Vue application, losing any state (like form inputs or counter values) and defeating the speed and seamlessness of a single-page app.

The solution is the <RouterLink> component. It looks and renders like an <a> tag, but it has special behavior:

  • It listens for clicks.
  • It prevents the browser's default behavior of making a new server request.
  • It updates the URL in the address bar using the browser's History API.
  • It tells Vue Router to render the new component inside the <RouterView>.

The following video brilliantly demonstrates the problem with <a> tags and how <RouterLink> solves it. It also shows you a bonus feature: how to style the currently active link.

Vue 3 Routing - Beginner to Advanced

This clip from the same 'Laiture' video is extremely valuable. It provides a perfect 'why' by showing state being lost with an <a> tag, and then preserved with <RouterLink>. This directly addresses your goal of understanding best practices.

Watch from 27:33 to 32:57. Pay close attention to the counter example that shows state loss. Also, note how the active-class prop is used to automatically style the link for the page you're currently on.



Part 3: Your Turn - Building the Main App Navigation

Let's apply these concepts to your personal profile project. You'll refactor your App.vue to create the main application shell with a persistent navigation bar.

1. Clean up and Structure App.vue
Open your src/App.vue file. If it has any content from previous exercises (like the temporary links to projects), remove it. We're building the final layout now.

Structure your template like this. For now, you don't need to import <RouterLink> or <RouterView> if you're using <script setup>; they are made globally available when you install the router.

// src/App.vue

<template>
  <div id="app-layout">
    <header class="app-header">
      <nav>
        <!-- Links will go here -->
      </nav>
    </header>

    <main class="app-main">
      <RouterView />
    </main>
  </div>
</template>

<style scoped>
/* Basic styling to get you started */
.app-header {
  background-color: #f8f9fa;
  padding: 1rem;
  border-bottom: 1px solid #dee2e6;
}

nav {
  display: flex;
  gap: 1rem;
  justify-content: center;
}

.app-main {
  padding: 2rem;
  max-width: 960px;
  margin: 0 auto;
}
</style>

2. Add Navigation Links with <RouterLink>
Inside the <nav> tags, add <RouterLink> components to navigate to your Home, About, and Contact pages. Use the to prop to specify the path.

// src/App.vue (inside the <nav>)
<nav>
  <RouterLink to="/">Home</RouterLink>
  <RouterLink to="/about">About</RouterLink>
  <RouterLink to="/contact">Contact</RouterLink>
</nav>

3. Style the Active Link
As you saw in the video, Vue Router automatically adds CSS classes to active links. The default class is router-link-active. Let's add a style to make it clear which page is currently active.

Add this to the <style> block in App.vue:

/* Add this inside your <style scoped> block */
nav a.router-link-active {
  font-weight: bold;
  color: #0d6efd;
  text-decoration: underline;
}

/* Optional: General link styling */
nav a {
  text-decoration: none;
  color: #333;
}

Now, run your application (npm run dev). You should see your navigation bar at the top. As you click between Home, About, and Contact, the content in the main area should change, the URL should update, and the active link should be styled—all without any page reloads!



Conclusion

You've now implemented the two most essential user-facing components of Vue Router. This structure is the foundation for almost any multi-page Vue application you will build.

Key Takeaways:

  • <RouterView /> is the placeholder component that renders the component corresponding to the current URL. It's the "canvas" for your pages.
  • <RouterLink to="..."> is the correct component for SPA navigation. It updates the URL and swaps components without a full page reload, preserving your application's state.
  • It's a best practice to place your main navigation and <RouterView> in your root App.vue component to create a persistent layout.
  • Vue Router helps with UX by automatically adding classes like router-link-active to the current link, making it easy to style.

In our next lesson, we'll finally connect all the pieces. You will learn to fetch data from a REST API within a component's onMounted lifecycle hook. We'll navigate to a dynamic project page (e.g., /projects/123), and use the id from the URL to fetch and display that specific project's data.