📖 Scenario: You are building a simple Vue app with two pages: Home and About. You want to add a button on the Home page that, when clicked, takes the user to the About page using Vue Router's programmatic navigation.
🎯 Goal: Create a Vue component for the Home page that uses useRouter to navigate to the About page when a button is clicked.
📋 What You'll Learn
Create a Vue component named HomeView with a button labeled 'Go to About'.
Import and use useRouter from vue-router inside the component's setup function.
Define a function named goToAbout that uses the router to navigate to the '/about' route.
Attach the goToAbout function to the button's click event.
💡 Why This Matters
🌍 Real World
Programmatic navigation is common in Vue apps when you want to move users between pages after actions like form submission or button clicks without using links.
💼 Career
Understanding how to use useRouter for navigation is essential for Vue developers building interactive single-page applications.
Progress0 / 4 steps
1
Create the HomeView component skeleton
Create a Vue component named HomeView with a template containing a button with the text 'Go to About'.
Vue
Hint
Start by writing the template with a button inside. No script code needed yet.
2
Import useRouter and create router instance
Inside the <script setup> block, import useRouter from vue-router and create a constant named router by calling useRouter().
Vue
Hint
Use ES module import syntax to import useRouter. Then call it to get the router instance.
3
Define the goToAbout function to navigate programmatically
Inside the <script setup> block, define a function named goToAbout that calls router.push('/about') to navigate to the About page.
Vue
Hint
Define a normal function that calls router.push with the path string.
4
Attach goToAbout to the button click event
In the template, add a @click event to the button that calls the goToAbout function when clicked.
Vue
Hint
Use Vue's event binding syntax @click on the button to call the function.
Practice
(1/5)
1. What does the useRouter function provide in a Vue 3 component?
easy
A. A method to style components dynamically
B. A way to fetch data from an API
C. Access to the router instance to navigate programmatically
D. A tool to manage component state
Solution
Step 1: Understand the purpose of useRouter
useRouter is a Vue Router composable that gives access to the router instance inside setup functions.
Step 2: Identify what the router instance allows
The router instance allows programmatic navigation, such as using router.push() to change routes.
Final Answer:
Access to the router instance to navigate programmatically -> Option C
Quick Check:
useRouter = router access [OK]
Hint: Remember: useRouter gives router control inside components [OK]
Common Mistakes:
Thinking useRouter fetches data
Confusing useRouter with state management
Assuming useRouter styles components
2. Which of the following is the correct syntax to navigate to the '/home' route using useRouter in Vue 3?
easy
A. const router = useRouter(); router.change('/home');
B. const router = useRouter(); router.push('/home');
C. const router = useRouter(); router.navigate('/home');
D. const router = useRouter(); router.go('/home');
Solution
Step 1: Recall the method to change routes programmatically
The correct method to navigate is router.push().
Step 2: Match the syntax with the correct method
Only router.push('/home') is valid; other methods like go, navigate, or change do not exist on the router instance.
Final Answer:
const router = useRouter(); router.push('/home'); -> Option B
Quick Check:
router.push() navigates routes [OK]
Hint: Use router.push() to navigate routes programmatically [OK]
A. useRouter is not called as a function, missing parentheses
B. router.push should use an object, not a string
C. goHome function is not returned from setup
D. router.push is not a valid method
Solution
Step 1: Check how useRouter is assigned
The code assigns const router = useRouter; without parentheses, so router is a reference to the function, not the router instance.
Step 2: Understand the impact on router.push call
Since router is not the router instance, calling router.push() will cause an error because router is not an object with push method.
Final Answer:
useRouter is not called as a function, missing parentheses -> Option A
Quick Check:
Call useRouter() with () to get router instance [OK]
Hint: Always call useRouter() with parentheses to get router [OK]
Common Mistakes:
Forgetting parentheses after useRouter
Using string instead of object incorrectly (not error here)
Not returning functions from setup (actually returned here)
5. You want to redirect users to the login page if they are not authenticated after clicking a button. Which Vue 3 setup code correctly uses useRouter for this programmatic navigation?
hard
A. const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.push('/login'); }
B. const router = useRouter; function checkAuth() { if (!isAuthenticated) router.push('/login'); }
C. const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.go('/login'); }
D. const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.navigate('/login'); }
Solution
Step 1: Confirm correct useRouter call
const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.push('/login'); } correctly calls useRouter() with parentheses to get the router instance.
Step 2: Verify correct navigation method
const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.push('/login'); } uses router.push('/login'), which is the correct method to navigate programmatically.
Final Answer:
const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.push('/login'); } -> Option A
Quick Check:
Call useRouter() and use router.push() for navigation [OK]
Hint: Call useRouter() and use router.push() to redirect [OK]
Common Mistakes:
Not calling useRouter as a function
Using router.go() or router.navigate() which don't exist
Forgetting to check authentication before navigation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.