I need to take a step-by-step approach to set up Vue Router. The first step is installing Vue Router using npm install vue-router, followed by configuring the router file and ensuring that app.use(router) is included in main.js.
Next, I’ll set up complex nested routes, especially those requiring authentication and roles. After that, I’ll implement global navigation guards to manage route access based on authentication and user roles effectively. Each parent layout will also utilize <router-view /> for rendering components.Structuring Vue Router Implementation
I need to explain using Pinia or Vuex for authentication alongside Vue Router. I'll touch on route-specific guards like beforeEnter for the admin route and in-component guards like beforeRouteUpdate and beforeRouteLeave.
Next, I’ll mention fetch guards and possibly use beforeRouteEnter or setup watchers. I need to keep the response concise while addressing the user’s request for complex routing, including relevant code snippets. The structure will include an introduction, baseline setup, defining layouts and nested routes, implementing global auth guards, and examples of named nested views.Clarifying Vue Versions
I want to specify that I'm assuming Vue 3 and Vue Router 4 for this implementation. It’s essential to clarify these versions to avoid confusion. Since there are differences with Vue 2, I’ll make sure to highlight those at the end for users who might be working with that version. Keeping the context clear helps in understanding the implementation better, and I want to provide a complete picture. Below is a complete example using Vue 3 + Vue Router 4 that shows:
Nested routes with layout components
Named/nested <router-view>s
Global guards (auth + roles)
Per-route guards
In-component guards
You can copy/paste and adapt.
1. Install and basic setup
bash
npm install vue-router
src/main.js:
js
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
createApp(App).use(router).mount('#app');
Keys in components correspond to name attributes on <router-view>. The unnamed view is default.
8. Lazy loading routes (good with complex routing)
To keep bundles small, load components on demand:
js
const DashboardHome = () => import('@/views/DashboardHome.vue');
const Profile = () => import('@/views/Profile.vue');
const AdminUsers = () => import('@/views/AdminUsers.vue');
const AdminSettings = () => import('@/views/AdminSettings.vue');
// ...use these functions in the routes instead of direct imports
9. If you’re on Vue 2
Main differences:
Install vue-router@3.
Use new Router({ ... }) instead of createRouter.
Use mode: 'history' instead of history: createWebHistory().
Example:
js
import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);
const router = new Router({
mode: 'history',
routes: [ /* same routes array structure */ ]
});
export default router;
If you share your current router config or a specific scenario (e.g. “I need a public marketing layout + authenticated app layout + admin-only subarea”), I can tailor the routing and guards exactly to that structure.