Router & State Questions with Answers

15 practice questions on Router & State (Vue.js) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

Which navigation guard runs before every navigation globally?

  1. router.afterAll
  2. router.beforeEach
  3. router.onEnter
  4. router.guard

Answer: B. router.beforeEach

Explanation

beforeEach((to, from) => ...) can return false or a location to cancel or redirect.

Step-by-step solution

  1. Register beforeEach.
  2. Return a result.

Why the other options are wrong

  • A. router.afterAll is not a Vue API for this; the correct choice is router.beforeEach.
  • C. router.onEnter is not a Vue API for this; the correct choice is router.beforeEach.
  • D. router.guard is not a Vue API for this; the correct choice is router.beforeEach.

Question 2Easy

Which Vue Router component renders the component matched by the current route?

  1. <RouterView>
  2. <RouterLink>
  3. <RouteOutlet>
  4. <Component>

Answer: A. <RouterView>

Explanation

<RouterView> is the placeholder where matched route components appear.

Step-by-step solution

  1. RouterView is the placeholder in the template where the matched route's component is shown.
  2. It renders whichever component matches the current route.
  3. So the component is RouterView (option A).

Why the other options are wrong

  • B. RouterLink creates navigation links, not the route's content.
  • C. RouteOutlet is not a Vue Router component.
  • D. Component is a dynamic component tag, not a Vue Router placeholder.

Tip: RouterView goes where the page content should appear; RouterLink goes on navigation items.

Question 3Easy

Which component is used to create navigation links in Vue Router that avoid full page reloads?

  1. <RouterLink>
  2. <a href>
  3. <NavTo>
  4. <RouterView>

Answer: A. <RouterLink>

Explanation

<RouterLink to="/about"> navigates client-side.

Step-by-step solution

  1. RouterLink is the Vue Router component for navigation links.
  2. It changes the route on the client, without a full page reload.
  3. So the component for navigation links is RouterLink (option A).

Why the other options are wrong

  • B. A plain a tag with href causes a full page load.
  • C. NavTo is not a Vue Router component.
  • D. RouterView displays the matched component; it is not a link.

Tip: Use RouterLink for internal links so navigation stays inside the app.

Question 4Easy

Which component renders the route's matched component?

  1. <RouteOutlet>
  2. <RouterView>
  3. <ViewPort>
  4. <RouterLink>

Answer: B. <RouterView>

Explanation

RouterView is the placeholder for route components.

Step-by-step solution

  1. Place RouterView.
  2. Matched view appears.

Why the other options are wrong

  • A. Angular name.
  • C. ViewPort is not a Vue Router component; RouterView renders the matched route.
  • D. RouterLink creates links.

Question 5Medium

What does a Pinia store created with defineStore return?

  1. The store directly, so you can import and use it without calling any function
  2. A Vuex module
  3. A function (e.g. useCounterStore) that returns the store instance
  4. A plain object

Answer: C. A function (e.g. useCounterStore) that returns the store instance

Explanation

Calling the returned useXStore() inside components yields the singleton store.

Step-by-step solution

  1. defineStore returns a hook.
  2. Call it to get the store.

Why the other options are wrong

  • A. It returns a hook.
  • B. Different library.
  • D. It is a reactive store.

Question 6Medium

What does router.push('/home') do versus router.replace('/home')?

  1. push swaps the current entry and replace adds a new one, so Back works in reverse
  2. Both reload the page
  3. push adds a history entry; replace swaps the current one
  4. Both do nothing for same path

Answer: C. push adds a history entry; replace swaps the current one

Explanation

After replace, the Back button skips the replaced entry.

Step-by-step solution

  1. push adds history.
  2. replace overwrites it.

Why the other options are wrong

  • A. Reversed.
  • B. SPA navigation does not reload.
  • D. Navigation still happens or duplicates are handled.

Question 7Medium

In a Pinia setup store, what plays the role of state and getters?

  1. data and methods
  2. this.state
  3. mutations
  4. ref() and computed()

Answer: D. ref() and computed()

Explanation

Setup stores use refs for state, computeds for getters and functions for actions.

Step-by-step solution

  1. Define refs.
  2. Define computeds.
  3. Return them.

Why the other options are wrong

  • A. Options API names.
  • B. A setup store does not use this.state; state is declared with ref().
  • C. Pinia has no mutations.

Question 8Medium

Which library is the official state management store for Vue 3?

  1. Redux
  2. Pinia
  3. MobX
  4. Flux

Answer: B. Pinia

Explanation

Pinia is the official store library for Vue; Vuex is the older one.

Step-by-step solution

  1. Pinia is the official state management library for Vue 3.
  2. Vuex is the older store library.
  3. So the official store for Vue 3 is Pinia (option B).

Why the other options are wrong

  • A. Redux is a React-focused library, not the official Vue store.
  • C. MobX is a separate state library, not the official Vue store.
  • D. Flux is an architecture pattern, not the official Vue store.

Tip: Pinia is the default choice for shared state in Vue 3.

Question 9Hard

Why does destructuring a Pinia store lose reactivity, and what is the fix?

  1. Primitive state is copied; use storeToRefs(store)
  2. Stores are frozen after creation, so you must clone them before reading properties
  3. Destructuring is blocked; use watch
  4. Use Object.assign

Answer: A. Primitive state is copied; use storeToRefs(store)

Explanation

storeToRefs produces refs for state and getters; actions can be destructured directly.

Step-by-step solution

  1. Destructure copies value.
  2. Use storeToRefs.

Why the other options are wrong

  • B. They are not frozen.
  • C. It is allowed but not reactive.
  • D. That also copies.

Question 10Hard

The component for /user/1 shows the same instance when going to /user/2. How do you react?

  1. Watch route.params.id (or use onBeforeRouteUpdate)
  2. Use onMounted only, because it runs each time the route parameters change
  3. Reload the page
  4. Use a v-if on the router

Answer: A. Watch route.params.id (or use onBeforeRouteUpdate)

Explanation

Vue Router reuses the component for the same route record, so mounted does not run again.

Step-by-step solution

  1. Instance reused.
  2. Watch params.

Why the other options are wrong

  • B. It will not run again; the instance is reused.
  • C. Defeats the SPA.
  • D. Does not detect param changes.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Vue.js topics · All topics