0
0
Vueframework~20 mins

Vue Router installation and setup - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Router Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you navigate to a route not defined in Vue Router?

Consider a Vue 3 app using Vue Router. The router has only two routes defined: '/' and '/about'. What will the app display if the user navigates to '/contact'?

Vue
import { createRouter, createWebHistory } from 'vue-router';
import Home from './components/Home.vue';
import About from './components/About.vue';

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

export default router;
AThe app automatically redirects to the '/' route.
BThe app shows a blank page because no route matches '/contact'.
CThe app shows the About component because it's the closest match.
DThe app throws a runtime error and crashes.
Attempts:
2 left
💡 Hint

Think about what Vue Router does when no route matches the URL.

📝 Syntax
intermediate
2:00remaining
Which code correctly installs Vue Router in a Vue 3 app?

You want to add Vue Router to your Vue 3 app. Which code snippet correctly sets up the router and integrates it with the app?

A
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

const app = createApp(App);
router.use(app);
app.mount('#app');
B
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

const app = createApp(App);
app.mount('#app');
app.use(router);
C
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

const app = createApp(App);
app.use(router).mount('#app');
D
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

const app = createApp(App);
app.use(router);
app.mount('#app');
Attempts:
2 left
💡 Hint

Remember the order: create app, use plugins, then mount.

state_output
advanced
2:00remaining
What is the value of $route.path after navigating programmatically?

In a Vue 3 app with Vue Router, you run router.push('/dashboard'). What will this.$route.path be immediately after?

Vue
import { createRouter, createWebHistory } from 'vue-router';
const routes = [{ path: '/dashboard', component: {} }];
const router = createRouter({ history: createWebHistory(), routes });

// Assume inside a component method:
async function navigate() {
  await router.push('/dashboard');
  console.log(this.$route.path);
}
A'/dashboard'
B'/'
Cundefined
DThrows an error because $route is undefined
Attempts:
2 left
💡 Hint

Think about what router.push does and when $route updates.

🔧 Debug
advanced
2:00remaining
Why does this Vue Router setup cause a blank page?

Look at this router setup code. The app shows a blank page on navigation. What is the cause?

Vue
import { createRouter, createWebHistory } from 'vue-router';
import Home from './Home.vue';

const routes = [
  { path: '/', component: Home }
];

const router = createRouter({
  history: createWebHistory('/app/'),
  routes
});

export default router;
AThe base URL '/app/' in createWebHistory does not match the actual app base, causing routing failure.
BThe Home component is not imported correctly, so nothing renders.
CcreateWebHistory requires no arguments; passing '/app/' causes a syntax error.
DThe routes array is empty, so no routes exist.
Attempts:
2 left
💡 Hint

Check if the history base matches the app's deployment path.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue Router's createWebHistory is true?

Choose the correct statement about createWebHistory in Vue Router.

A<code>createWebHistory</code> appends a hash (#) to URLs to manage routing.
B<code>createWebHistory</code> automatically falls back to hash mode if the browser does not support History API.
C<code>createWebHistory</code> uses the HTML5 History API and requires server configuration to handle fallback routes.
D<code>createWebHistory</code> disables browser back and forward buttons to control navigation.
Attempts:
2 left
💡 Hint

Think about how URLs look and what the server needs to do.