0
0
Vueframework~20 mins

Programmatic navigation with useRouter in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Programmatic Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Vue component calls router.push?

Consider this Vue 3 component using useRouter to navigate:

import { useRouter } from 'vue-router';
import { defineComponent } from 'vue';

export default defineComponent({
  setup() {
    const router = useRouter();
    function goHome() {
      router.push('/home');
    }
    return { goHome };
  }
});

What is the result when goHome() is called?

Vue
import { useRouter } from 'vue-router';
import { defineComponent } from 'vue';

export default defineComponent({
  setup() {
    const router = useRouter();
    function goHome() {
      router.push('/home');
    }
    return { goHome };
  }
});
AThe browser URL changes to '/home' and the app navigates to the Home route without reloading the page.
BAn error is thrown because useRouter cannot be used inside setup.
CNothing happens because router.push requires a callback to work.
DThe browser reloads the entire page and then navigates to '/home'.
Attempts:
2 left
💡 Hint

Think about how Vue Router handles navigation programmatically.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses useRouter to navigate to a named route with params?

Given a route named 'user' that expects a param id, which code correctly navigates to user with id 42?

Arouter.push({ name: 'user', query: { id: 42 } })
Brouter.push('/user/42')
Crouter.push({ name: 'user', params: { id: 42 } })
Drouter.push({ path: 'user', params: { id: 42 } })
Attempts:
2 left
💡 Hint

Named routes use name and params keys.

🔧 Debug
advanced
2:00remaining
Why does this programmatic navigation fail to update the URL?

Look at this code snippet inside a Vue component:

const router = useRouter();
function goProfile() {
  router.push('/profile');
}

// Called on button click

But clicking the button does not change the URL or route. Why?

Vue
const router = useRouter();
function goProfile() {
  router.push('/profile');
}

// Called on button click
<button @click="goProfile">Go</button>
AThe route '/profile' is not defined in the router configuration.
Brouter.push requires await to work properly.
CuseRouter cannot be used inside setup(), it must be imported globally.
DThe <code>goProfile</code> function is not exposed in the component's template context.
Attempts:
2 left
💡 Hint

Check if the function is accessible in the template.

state_output
advanced
2:00remaining
What is the value of currentRoute.path after this navigation?

Given this code inside a Vue 3 component:

import { useRouter, useRoute } from 'vue-router';

const router = useRouter();
const currentRoute = useRoute();

await router.push('/dashboard');

console.log(currentRoute.path);

What will be logged?

Vue
import { useRouter, useRoute } from 'vue-router';

const router = useRouter();
const currentRoute = useRoute();

await router.push('/dashboard');

console.log(currentRoute.path);
A'/dashboard'
BThe old path before navigation
Cundefined
DAn error because currentRoute is not reactive
Attempts:
2 left
💡 Hint

Think about reactivity and when currentRoute updates.

🧠 Conceptual
expert
2:00remaining
Which statement about useRouter and programmatic navigation is true?

Choose the correct statement about useRouter and programmatic navigation in Vue 3.

ACalling <code>router.push()</code> always reloads the entire page.
B<code>useRouter</code> can only be used inside <code>setup()</code> or lifecycle hooks of components.
C<code>useRouter</code> returns a new router instance every time it is called.
DProgrammatic navigation with <code>router.push()</code> does not update the browser URL.
Attempts:
2 left
💡 Hint

Think about where useRouter is designed to be used.