Using <code>RouterView</code> to Render Pages in Vue
📖 Scenario: You are building a simple Vue app that shows different pages when users navigate. You will use Vue Router's RouterView component to display the current page content.
🎯 Goal: Create a Vue app with a router and use RouterView to render the matched page component inside the main layout.
📋 What You'll Learn
Create a basic Vue app with a router
Define two simple page components: Home and About
Set up routes for '/' and '/about' paths
Use RouterView in the main App component to show the current page
💡 Why This Matters
🌍 Real World
Most Vue apps use Vue Router to handle navigation between pages or views. Using <code>RouterView</code> lets you display the correct page content based on the URL.
💼 Career
Understanding how to use Vue Router and <code>RouterView</code> is essential for frontend developers building single-page applications with Vue.
Progress0 / 4 steps
1
Set up Vue Router with two routes
Create a router instance in a file called router.js with two routes: { path: '/', component: Home } and { path: '/about', component: About }. Import createRouter and createWebHistory from 'vue-router'. Also, create two simple components called Home and About as empty objects for now.
Vue
Hint
Remember to import createRouter and createWebHistory from 'vue-router'. Define the routes array with the two paths and components. Then create the router instance and export it.
2
Create simple Home and About components
Replace the empty Home and About objects with Vue components using the defineComponent function. Each component should have a template property with a <h1> heading: 'Home Page' for Home and 'About Page' for About. Import defineComponent from 'vue'.
Vue
Hint
Use defineComponent to create components with a simple template string showing the page title.
3
Use RouterView in the main App component
Create a Vue component called App using defineComponent. Inside its template, add a <nav> with two <a> links: one to / labeled 'Home' and one to /about labeled 'About'. Below the navigation, add the <RouterView /> component to render the current route's page. Import RouterView from 'vue-router'.
Vue
Hint
Use RouterView inside the template to show the current page. Add navigation links with <a> tags.
4
Create Vue app and mount with router and App component
In your main entry file (e.g., main.js), import createApp from 'vue', import the App component and the router from router.js. Create the Vue app with createApp(App), use the router with app.use(router), and mount the app to an element with id app.
Vue
Hint
Remember to create the app, add the router with use, and mount it to the element with id app.
Practice
(1/5)
1. What is the main purpose of RouterView in a Vue application?
easy
A. To define routes and their components
B. To display the component that matches the current route URL
C. To style the navigation bar
D. To fetch data from an API
Solution
Step 1: Understand RouterView role
RouterView is a placeholder that Vue Router uses to show the component for the current URL.
Step 2: Differentiate from other router parts
Defining routes is done in the router config, not in RouterView. Styling and data fetching are unrelated.
Final Answer:
To display the component that matches the current route URL -> Option B
Quick Check:
RouterView shows current page = A [OK]
Hint: RouterView shows the page for the URL [OK]
Common Mistakes:
Confusing RouterView with route definitions
Thinking RouterView styles navigation
Assuming RouterView fetches data
2. Which of the following is the correct way to use RouterView in a Vue component template?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Vue Router syntax
The official tag for rendering matched components is <router-view> in kebab-case.
Step 2: Check case sensitivity
Vue components and tags are case-sensitive. RouterView (PascalCase) is not valid in templates, nor is routerView or router_view.
Final Answer:
<router-view></router-view> -> Option C
Quick Check:
Use kebab-case tag [OK]
Hint: Use lowercase with dash: [OK]
Common Mistakes:
Using PascalCase <RouterView> in template
Using camelCase or underscores in tag name
Forgetting closing tag
3. Given this Vue Router setup and component usage, what will be rendered inside <router-view> when the URL is /about?
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];
// In App.vue template:
// <router-view></router-view>
medium
A. The About component content
B. The Home component content
C. Nothing, because no component is matched
D. An error because routes are not defined properly
Solution
Step 1: Match URL to route
The URL /about matches the route with path /about which uses the About component.
Step 2: Understand RouterView rendering
<router-view> renders the component for the matched route, so it will show About's content.
Final Answer:
The About component content -> Option A
Quick Check:
URL /about shows About component [OK]
Hint: RouterView shows component for current URL [OK]
Common Mistakes:
Assuming root component always shows
Thinking RouterView shows nothing if not root
Believing routes need special syntax to match
4. What is wrong with this Vue component template using RouterView?
A. The RouterView tag should be lowercase as router-view
B. RouterView cannot be self-closing
C. RouterView must be inside a <router> tag
D. RouterView requires a slot to render content
Solution
Step 1: Check tag casing rules
In Vue templates, the router-view component must be written in kebab-case, all lowercase with a dash.
Step 2: Validate other options
Self-closing is allowed in Vue 3. There is no <router> tag, and router-view does not require slots to render matched components.
Final Answer:
The <RouterView> tag should be lowercase as <router-view> -> Option A
Quick Check:
Use lowercase tag [OK]
Hint: Use lowercase , not PascalCase [OK]
Common Mistakes:
Writing <RouterView> instead of
Thinking self-closing is invalid
Expecting a <router> wrapper tag
5. You want to create a nested route in Vue where /dashboard shows a Dashboard component and inside it, /dashboard/settings shows a Settings component. How should you use RouterView to render the nested Settings component inside Dashboard?
hard
A. Place <router-view> only in the main App.vue template
B. Nest <router-view> inside <router-link> in Dashboard
C. Use two <router-view> tags side by side in App.vue
D. Place <router-view> inside the Dashboard component template where Settings should appear
Solution
Step 1: Understand nested routes rendering
Nested routes render their child components inside the parent's RouterView. So Dashboard must have a <router-view> to show Settings.
Step 2: Check other options
Placing RouterView only in App.vue won't render nested children inside Dashboard. Two side-by-side RouterView tags or nesting inside router-link are incorrect usage.
Final Answer:
Place <router-view> inside the Dashboard component template where Settings should appear -> Option D
Quick Check:
Nested routes need RouterView inside parent [OK]
Hint: Put inside parent component for nested routes [OK]
Common Mistakes:
Not placing RouterView inside parent component
Trying to render nested routes only in App.vue
Misusing router-link as container
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.