Performance: Vue Router installation and setup
This affects the initial page load speed and navigation responsiveness by adding routing logic and components to the Vue app.
import { createApp } from 'vue'; import { createRouter, createWebHistory } from 'vue-router'; import App from './App.vue'; 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 }); const app = createApp(App); app.use(router); app.mount('#app');
import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); app.mount('#app');
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No Router (full reloads) | High (full page reload) | Many (full page layout) | High (full repaint) | [X] Bad |
| Vue Router with eager loading | Moderate (component swaps) | Few (component layout changes) | Moderate (partial repaint) | [!] OK |
| Vue Router with lazy loading | Low (only loaded components) | Few (minimal layout) | Low (minimal repaint) | [OK] Good |
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;
And in App.vue template:
<router-view />
import { createRouter, createWebHistory } from 'vue-router';
import Home from './components/Home.vue';
import Contact from './components/Contact.vue';
const routes = [
{ path: '/', component: Home },
{ path: '/contact', component: Contact }
];
const router = createRouter({
history: createWebHistory,
routes
});
export default router;Pass the test and this pattern moves unknown