Question 14
Consider the below Vue router setup.
Note: Assume that the API “/api/users/userId” is operational and returns the user’s data including name.
const UserProfile = { template: `<div><h2>User Profile</h2><p>{{ user.name }}</p></div>`, data() { return { user: {} }; }, created() { this.fetchUserData(); }, methods: { async fetchUserData() { const userId = this.$route.query.userId; const response = await fetch(`/api/users/${userId}`); this.user = await response.json(); } }};
const routes = [ { path: '/user', component: UserProfile }];
const router = new VueRouter({ routes});What will happen if the user navigates to “/user?userId=123”?
The component will display the user's name (fetched from API) correctly as expected.
The component will show because the API call will never be triggered.
The component will always show an empty profile because the userId query parameter is not reactive.
The component will throw an error because this.$route.query is not defined.