0
0
Vueframework~20 mins

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

Choose your learning style9 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.