Most modern web apps use SPAs to provide smooth user experiences without full page reloads. Routing is essential to switch views inside the app.
💼 Career
Understanding routing in Vue is key for frontend developer roles building interactive web applications.
Progress0 / 4 steps
1
Create the initial Vue app structure
Create a Vue app with a root App component that contains a <template> with a <div> and a <router-view> tag inside it.
Vue
Hint
Use the <router-view> tag inside your root component to show the current page content.
2
Add route configuration
Create a constant called routes that is an array with three route objects. Each object should have a path and a component property. Use paths '/', '/products', and '/contact'. For components, use simple objects with a template string: '
Home Page
', '
Products Page
', and '
Contact Page
' respectively.
Vue
Hint
Define routes as an array of objects with path and component keys.
3
Create the Vue Router instance
Import createRouter and createWebHistory from 'vue-router'. Then create a constant called router by calling createRouter with an object that has history set to createWebHistory() and routes set to the routes constant.
Vue
Hint
Use createRouter with history and routes keys to set up routing.
4
Complete the app by mounting router and adding navigation
Import createApp from 'vue'. Create the app with createApp(App). Use app.use(router) to add the router. Mount the app to #app. Also, inside the <template> add a <nav> with three <router-link> tags with to attributes set to '/', '/products', and '/contact' respectively, and text 'Home', 'Products', and 'Contact'.
Vue
Hint
Add navigation links with <router-link> and mount the Vue app with the router.
Practice
(1/5)
1. Why do Single Page Applications (SPAs) need routing? Routing in SPAs helps to:
easy
A. Change views without reloading the whole page
B. Reload the entire website every time a link is clicked
C. Make the website slower by adding extra pages
D. Disable navigation between different parts of the app
Solution
Step 1: Understand SPA behavior
SPAs load a single HTML page and dynamically update content without full reloads.
Step 2: Role of routing in SPAs
Routing allows switching views inside the SPA without refreshing the browser, improving speed and user experience.
Final Answer:
Change views without reloading the whole page -> Option A
Quick Check:
Routing in SPAs = Change views without reload [OK]
Hint: Routing changes views inside SPA without page reload [OK]
Common Mistakes:
Thinking SPAs reload the whole page on navigation
Confusing routing with server-side page loads
Believing routing disables navigation
2. Which of the following is the correct way to define a route in Vue Router?
easy
A. const routes = { '/home': HomePage }
B. const routes = [{ path: '/home', component: HomePage }]
C. const routes = ['/home', HomePage]
D. const routes = [path: '/home', component: HomePage]
Solution
Step 1: Recall Vue Router route syntax
Routes are defined as an array of objects with path and component keys.
Step 2: Match syntax with options
const routes = [{ path: '/home', component: HomePage }] correctly uses an array with an object containing path and component.