Bird
Raised Fist0
Vueframework~20 mins

Route parameters with useRoute in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Route Params Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Vue component display when the route parameter 'id' is '42'?

Consider this Vue 3 component using useRoute to access route parameters.

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

export default defineComponent({
  setup() {
    const route = useRoute();
    return { id: route.params.id };
  },
  template: `
User ID: {{ id }}
` });

If the current route is /user/42 where id is a route param, what will be rendered?

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

export default defineComponent({
  setup() {
    const route = useRoute();
    return { id: route.params.id };
  },
  template: `<div>User ID: {{ id }}</div>`
});
A<div>User ID: 42</div>
B<div>User ID: {{ id }}</div>
C<div>User ID: undefined</div>
D<div>User ID: </div>
Attempts:
2 left
💡 Hint

Remember that useRoute() gives access to the current route's parameters.

📝 Syntax
intermediate
2:00remaining
Which option correctly accesses a nested route parameter 'postId' using useRoute?

Given a nested route with parameter postId, which code correctly extracts it inside setup()?

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

export default {
  setup() {
    // Which option correctly gets postId?
  }
}
Aconst route = useRoute; const postId = route.params.postId;
Bconst { postId } = useRoute.params;
Cconst route = useRoute(); const postId = route.params.postId;
Dconst postId = useRoute().params.postId;
Attempts:
2 left
💡 Hint

Remember useRoute is a function that returns the route object.

❓ state_output
advanced
2:00remaining
What is the value of 'userId' after route changes from '/user/1' to '/user/5' in this component?

Consider this Vue 3 component that uses useRoute and a watch to update userId:

import { ref, watch } from 'vue';
import { useRoute } from 'vue-router';

export default {
  setup() {
    const route = useRoute();
    const userId = ref(route.params.id);

    watch(() => route.params.id, (newId) => {
      userId.value = newId;
    });

    return { userId };
  }
};

If the route changes from /user/1 to /user/5, what will be the value of userId.value?

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

export default {
  setup() {
    const route = useRoute();
    const userId = ref(route.params.id);

    watch(() => route.params.id, (newId) => {
      userId.value = newId;
    });

    return { userId };
  }
};
A5
B1
Cundefined
Dnull
Attempts:
2 left
💡 Hint

The watch updates userId when the route param changes.

🔧 Debug
advanced
2:00remaining
Why does this component show 'undefined' for route param 'productId'?

Look at this Vue 3 component:

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

export default defineComponent({
  setup() {
    const route = useRoute;
    return { productId: route.params.productId };
  },
  template: `
Product: {{ productId }}
` });

When the route is /product/10, the component shows Product: undefined. Why?

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

export default defineComponent({
  setup() {
    const route = useRoute;
    return { productId: route.params.productId };
  },
  template: `<div>Product: {{ productId }}</div>`
});
ABecause productId is not defined in the route params.
BBecause useRoute must be imported from 'vue' not 'vue-router'.
CBecause the template syntax is incorrect and does not interpolate productId.
DBecause useRoute is not called as a function, so route is the function itself, not the route object.
Attempts:
2 left
💡 Hint

Check how useRoute is used in setup().

🧠 Conceptual
expert
3:00remaining
Which statement about useRoute and reactive route params is true?

Consider the following statements about useRoute in Vue Router:

  1. useRoute() returns a reactive route object.
  2. Route params accessed via useRoute().params are reactive and update automatically.
  3. Directly destructuring params like const { id } = useRoute().params keeps id reactive.
  4. Watching useRoute().params.id is the recommended way to react to param changes.

Which is the only true statement?

AOnly statement 3 is true; destructuring keeps reactivity but useRoute is not reactive.
BOnly statements 1 and 2 are true; destructuring breaks reactivity, and watching params.id is correct.
CAll statements are true; destructuring keeps reactivity and watching params.id works fine.
DOnly statement 4 is true; useRoute is not reactive but watching params.id works.
Attempts:
2 left
💡 Hint

Think about how destructuring affects reactivity in Vue.

Practice

(1/5)
1. What does the useRoute function in Vue Router allow you to do inside a component?
easy
A. Access the current route's parameters and information
B. Change the URL path programmatically
C. Register a new route dynamically
D. Navigate to a different route without reloading

Solution

  1. Step 1: Understand the purpose of useRoute

    useRoute is a Vue Router composable that provides access to the current route object inside a component.
  2. Step 2: Identify what can be accessed with useRoute

    It allows reading route parameters, query, name, and path but does not change routes or register them.
  3. Final Answer:

    Access the current route's parameters and information -> Option A
  4. Quick Check:

    useRoute reads route info = B [OK]
Hint: useRoute reads route info, doesn't change or register routes [OK]
Common Mistakes:
  • Thinking useRoute changes routes
  • Confusing useRoute with useRouter
  • Assuming useRoute registers routes
2. Which of the following is the correct way to import and use useRoute in a Vue 3 component's setup() function?
easy
A. import { useRoute } from 'vue-router'; const route = useRoute(route);
B. import useRoute from 'vue-router'; const route = useRoute();
C. import { useRoute } from 'vue-router'; const route = useRoute();
D. import { useRoute } from 'vue'; const route = useRoute();

Solution

  1. Step 1: Check the import source

    useRoute is exported from 'vue-router', so import must be from 'vue-router'.
  2. Step 2: Verify usage syntax

    The correct usage is calling useRoute() without arguments to get the current route object.
  3. Final Answer:

    import { useRoute } from 'vue-router'; const route = useRoute(); -> Option C
  4. Quick Check:

    Correct import and call = D [OK]
Hint: Import from 'vue-router' and call useRoute() with no arguments [OK]
Common Mistakes:
  • Importing from 'vue' instead of 'vue-router'
  • Calling useRoute with arguments
  • Using default import instead of named import
3. Given this Vue 3 component code snippet, what will be logged when the URL is /user/42 and the route is defined as /user/:id?
import { useRoute } from 'vue-router';
import { onMounted } from 'vue';

export default {
  setup() {
    const route = useRoute();
    onMounted(() => {
      console.log(route.params.id);
    });
  }
}
medium
A. null
B. undefined
C. user
D. 42

Solution

  1. Step 1: Identify the route parameter

    The route is defined as /user/:id, so id is a parameter expected in route.params.
  2. Step 2: Check the URL and logged value

    The URL is /user/42, so route.params.id will be "42". The console logs this value on mount.
  3. Final Answer:

    42 -> Option D
  4. Quick Check:

    route.params.id = "42" [OK]
Hint: Route param matches URL segment after colon :id [OK]
Common Mistakes:
  • Expecting param name instead of value
  • Confusing params with query
  • Logging before route is ready
4. What is the main issue with this Vue 3 component code that tries to react to route parameter changes?
import { useRoute } from 'vue-router';
import { computed } from 'vue';

export default {
  setup() {
    const route = useRoute();
    const userId = computed(() => route.params.id);
    return { userId };
  }
}
The component does not update userId when the route changes.
medium
A. The route object is not reactive, so computed won't update
B. The computed function is missing a dependency array
C. The useRoute hook must be called inside onMounted
D. The userId should be a ref, not computed

Solution

  1. Step 1: Understand reactivity of route

    The route object returned by useRoute() is reactive, but its params property is not deeply reactive by default.
  2. Step 2: Why computed does not update

    Because route.params is not reactive, the computed property does not track changes, so userId stays the same.
  3. Final Answer:

    The route object is not reactive, so computed won't update -> Option A
  4. Quick Check:

    Route params not reactive = A [OK]
Hint: Route params are not deeply reactive; watch route.fullPath instead [OK]
Common Mistakes:
  • Assuming route.params is reactive
  • Calling useRoute inside lifecycle hooks
  • Using ref instead of computed incorrectly
5. You want to create a Vue 3 component that displays the user ID from the route parameter and updates automatically when the route changes. Which approach correctly achieves this?
import { useRoute } from 'vue-router';
import { computed, watch, ref } from 'vue';

export default {
  setup() {
    const route = useRoute();
    // What should userId be?
    const userId = ???
    return { userId };
  }
}
hard
A. computed(() => route.params.id)
B. computed(() => route.params.id) with a watch on route.fullPath to trigger updates
C. computed(() => route.fullPath.split('/')[2])
D. ref(route.params.id) without any watcher

Solution

  1. Step 1: Understand reactivity limitations

    Route params are not deeply reactive, so computed(() => route.params.id) alone won't update when params change.
  2. Step 2: Use a watch on route.fullPath to trigger updates

    Watching route.fullPath (which is reactive) and recomputing userId ensures updates when the route changes.
  3. Final Answer:

    computed(() => route.params.id) with a watch on route.fullPath to trigger updates -> Option B
  4. Quick Check:

    Watch route.fullPath for param changes = C [OK]
Hint: Watch route.fullPath to react to param changes in computed [OK]
Common Mistakes:
  • Using computed alone without watch
  • Parsing fullPath manually without reactivity
  • Using ref without updating on route change
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard