📖 Scenario: You are building a simple Vue app with navigation links to switch between pages without reloading the browser.
🎯 Goal: Create a Vue component that uses RouterLink to navigate between a Home page and an About page.
📋 What You'll Learn
Create a Vue component with a navigation menu
Use RouterLink components to link to /home and /about routes
Add a router-view to display the current page content
Use the Composition API with <script setup>
💡 Why This Matters
🌍 Real World
Navigation menus are essential in web apps to let users move between pages smoothly without full page reloads.
💼 Career
Understanding RouterLink and router-view is key for frontend developers working with Vue to build single-page applications.
Progress0 / 4 steps
1
Set up the Vue component skeleton
Create a Vue component file with <template> and <script setup> tags. Inside the template, add a <nav> element for navigation links.
Vue
Hint
Start by creating the basic Vue component structure with template and script setup tags.
2
Add RouterLink components for navigation
Inside the <nav> element, add two RouterLink components. One should link to /home with text Home, and the other to /about with text About. Import RouterLink from vue-router in the <script setup> section.
Vue
Hint
Use RouterLink components with the to attribute to create navigation links.
3
Add router-view to display page content
Below the <nav> element, add a <router-view> element inside the template. Import RouterView from vue-router in the <script setup> section.
Vue
Hint
The router-view element shows the current page component based on the route.
4
Complete the component with accessibility and structure
Add an aria-label attribute with value Primary navigation to the <nav> element for accessibility. Ensure the component uses semantic HTML and the navigation links are keyboard accessible.
Vue
Hint
Adding aria-label helps screen readers understand the navigation region.
Practice
(1/5)
1. What is the main purpose of the RouterLink component in Vue?
easy
A. To fetch data from an API
B. To style buttons with CSS
C. To create clickable links that navigate between pages without reloading
D. To manage application state
Solution
Step 1: Understand RouterLink's role
The RouterLink component is designed to create links that navigate between different pages or views in a Vue app.
Step 2: Compare with other options
Fetching data, styling buttons, and managing state are unrelated to navigation links.
Final Answer:
To create clickable links that navigate between pages without reloading -> Option C
Quick Check:
RouterLink = navigation links [OK]
Hint: RouterLink is for navigation links, not data or styling [OK]
Common Mistakes:
Confusing RouterLink with data fetching methods
Thinking RouterLink manages app state
Assuming RouterLink styles elements
2. Which of the following is the correct syntax to create a RouterLink to the path '/home'?
easy
A. Home
B. Home
C. Home
D. Home
Solution
Step 1: Recall RouterLink attribute for navigation
The correct attribute to specify the destination path is to, not href, link, or path.
Step 2: Verify syntax correctness
Using to='/home' inside <RouterLink> is the standard Vue Router syntax.
Final Answer:
<RouterLink to='/home'>Home</RouterLink> -> Option A
Quick Check:
Use 'to' attribute for RouterLink path [OK]
Hint: RouterLink uses 'to' attribute, not 'href' [OK]
Common Mistakes:
Using 'href' instead of 'to'
Using non-existent attributes like 'link' or 'path'
Forgetting to close the RouterLink tag properly
3. Given this Vue template snippet:
<RouterLink to='/about'>About</RouterLink>
What happens when the user clicks this link?
medium
A. The page reloads and navigates to '/about'
B. Nothing happens because RouterLink requires a click handler
C. An error is thrown because 'to' is not a valid attribute
D. The app navigates to '/about' without reloading the page
Solution
Step 1: Understand RouterLink navigation behavior
RouterLink navigates within the Vue app using client-side routing, so it changes the page without a full reload.
Step 2: Check attribute validity
The to attribute is valid and specifies the target route.
Final Answer:
The app navigates to '/about' without reloading the page -> Option D
Quick Check:
RouterLink navigates without reload [OK]
Hint: RouterLink changes page without reload on click [OK]
Common Mistakes:
Thinking RouterLink causes full page reload
Believing RouterLink needs extra click handlers
Confusing 'to' attribute as invalid
4. Identify the error in this RouterLink usage:
<RouterLink to='/contact'>Contact</RouterLink>
and explain why it might not work as expected.
medium
A. RouterLink must be imported and registered before use
B. The 'to' attribute should be 'href' for RouterLink
C. RouterLink requires a closing slash like <RouterLink />
D. The text inside RouterLink must be wrapped in a span
Solution
Step 1: Check attribute correctness
The 'to' attribute is correct for RouterLink; 'href' is incorrect.
Step 2: Verify component usage requirements
RouterLink must be imported from 'vue-router' and registered or globally available to work properly.
Final Answer:
RouterLink must be imported and registered before use -> Option A
Quick Check:
RouterLink needs import and registration [OK]
Hint: Always import and register RouterLink before using [OK]
Common Mistakes:
Using 'href' instead of 'to'
Forgetting to import RouterLink
Thinking RouterLink needs self-closing tag
5. You want to style the active RouterLink differently. Which attribute or feature should you use to apply a CSS class when the link is active?
hard
A. Add a click event to change the class manually
B. Use the 'active-class' attribute on RouterLink
C. Use the 'class' attribute with a fixed class name
D. Wrap RouterLink in a div and style the div
Solution
Step 1: Recall RouterLink active styling feature
RouterLink supports an active-class attribute that automatically applies a CSS class when the link matches the current route.
Step 2: Compare with other options
Manually changing classes or wrapping in divs is unnecessary and less efficient.
Final Answer:
Use the 'active-class' attribute on RouterLink -> Option B
Quick Check:
Active link styling uses 'active-class' [OK]
Hint: Use 'active-class' to style active RouterLink automatically [OK]
Common Mistakes:
Trying to style active links manually with click events
Using fixed class without active detection
Styling parent div instead of RouterLink
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.