0
0
Vueframework~10 mins

Vue Router installation and setup - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Vue Router installation and setup
Start Project Setup
Install Vue Router
Create Router Instance
Define Routes Array
Add Router to Vue App
Mount Vue App with Router
App Ready
This flow shows the steps to add Vue Router: install it, create routes, add router to app, then mount the app.
Execution Sample
Vue
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'

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

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

const app = createApp(App)
app.use(router)
app.mount('#app')
This code defines two routes, creates the router, adds it to the Vue app, and mounts the app.
Execution Table
StepActionCode LineResultNotes
1Import createRouter and createWebHistoryimport { createRouter, createWebHistory } from 'vue-router'Functions ready to usePrepare router creation tools
2Define routes arrayconst routes = [...]Routes array with '/' and '/about'Routes tell router which component to show
3Create router instanceconst router = createRouter({ history: createWebHistory(), routes })Router object createdRouter now knows history mode and routes
4Create Vue app instanceconst app = createApp(App)Vue app instance createdApp ready to use plugins
5Add router to appapp.use(router)Router plugin added to appApp can now handle routing
6Mount app to DOMapp.mount('#app')App appears on page with routingApp is live and interactive
7ExitN/ASetup completeVue app with router is ready
💡 All steps done, Vue Router installed and app mounted successfully
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
createRouterimported functionimported functionimported functionimported functionimported function
createWebHistoryimported functionimported functionimported functionimported functionimported function
routesundefined[{path:'/',component:Home},{path:'/about',component:About}][{path:'/',component:Home},{path:'/about',component:About}][{path:'/',component:Home},{path:'/about',component:About}][{path:'/',component:Home},{path:'/about',component:About}]
routerundefinedundefinedrouter instancerouter instancerouter instance
appundefinedundefinedundefinedVue app instanceVue app instance
Key Moments - 2 Insights
Why do we need to call app.use(router) before mounting the app?
Calling app.use(router) registers the router plugin with the Vue app so routing works. Without it, the app won't respond to route changes. See execution_table step 5.
What does createWebHistory() do in the router setup?
createWebHistory() sets the router to use browser history mode for clean URLs without hashes. It's passed to createRouter as the history option. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'routes' after step 2?
Arouter instance
Bundefined
C[{path:'/',component:Home},{path:'/about',component:About}]
DVue app instance
💡 Hint
Check the 'routes' row in variable_tracker after step 2
At which step is the Vue app instance created?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'app' variable in variable_tracker and execution_table actions
If we skip app.use(router), what happens when mounting the app?
AApp mounts but routing won't work
BApp fails to mount
CRouter works without registration
DApp shows an error immediately
💡 Hint
Refer to key_moments about app.use(router) importance
Concept Snapshot
Vue Router Setup Quick Steps:
1. Install Vue Router package.
2. Import createRouter and createWebHistory.
3. Define routes array with path and component.
4. Create router with createRouter({ history, routes }).
5. Create Vue app and add router with app.use(router).
6. Mount app with app.mount('#app').
Full Transcript
To set up Vue Router, first install it in your project. Then import createRouter and createWebHistory from 'vue-router'. Define your routes as an array of objects with path and component. Create the router instance by calling createRouter with history set to createWebHistory() and the routes array. Next, create your Vue app instance with createApp. Add the router to the app using app.use(router). Finally, mount the app to a DOM element with app.mount('#app'). This process enables your Vue app to handle navigation between pages smoothly.