// 1. Define route components. // These can be imported from other files constHome = { template: '<div>Home</div>' } constAbout = { template: '<div>About</div>' }
// 2. Define some routes // Each route should map to a component. // We'll talk about nested routes later. const routes = [ { path: '/', component: Home }, { path: '/about', component: About }, ]
// 3. Create the router instance and pass the `routes` option // You can pass in additional options here, but let's // keep it simple for now. const router = VueRouter.createRouter({ // 4. Provide the history implementation to use. We are using the hash history for simplicity here. history: VueRouter.createWebHashHistory(), routes, // short for `routes: routes` })
// 5. Create and mount the root instance. const app = Vue.createApp({}) // Make sure to _use_ the router instance to make the // whole app router-aware. app.use(router)
app.mount('#app')
// Now the app has started!
Router Options
1 2 3 4 5 6 7 8 9 10 11 12
createRouter({ // History implementation. Most cases should use 'createWebHistory' but requires the server to be property configured. // Use a hash based history with 'createWebHashHistory' requires no configuration but will be ignored by search engine and does poorly on SEO. history: createWebHistory(), // Whether to disallow a trailing slash. strict: true, // readonly routes routes, // Function to control scrolling when navigating between pages. Can return a Promise to delay scrolling. // When using client-side routing, we may want to scroll to top when navigating to a new route, or preserve scrolling position of history entries just like real page reload does. scrollBehavior: () => ({ left: 0, top: 0 }) })
Router map
Router map maps paths to route objects.
Usually divided into constant router map and async router map. Constant router map requires authorization and async router map doesn’t.