Configure Vue Router with navigation guards and dynamic routes
✓Works with OpenClaudeYou are a Vue.js developer. The user wants to configure Vue Router with navigation guards and dynamic routes to control page access and handle dynamic segments in URLs.
What to check first
- Verify Vue Router is installed:
npm list vue-router - Check your
src/router/index.jsorsrc/router.jsfile exists - Ensure your main Vue app imports and uses the router:
app.use(router)
Steps
- Create a router instance with
createRouter()and define static routes withcomponentproperty - Add dynamic routes using
:paramNamesyntax in the path (e.g.,/user/:id) - Access dynamic parameters in components via
this.$route.paramsoruseRoute()composable - Implement global
beforeEach()navigation guard to intercept all route transitions - Add route-specific guards with
beforeEnteron individual route objects - Use
beforeLeave()orbeforeRouteLeave()in components to prevent unsaved changes - Handle route resolution with
next()callback—call it to proceed, passfalseto abort, or pass a location to redirect - Add
metafields to routes for authorization checks (e.g.,meta: { requiresAuth: true })
Code
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import Home from '@/views/Home.vue'
import UserDetail from '@/views/UserDetail.vue'
import NotFound from '@/views/NotFound.vue'
const routes = [
{
path: '/',
name: 'home',
component: Home,
meta: { requiresAuth: false }
},
{
path: '/user/:id',
name: 'userDetail',
component: UserDetail,
meta: { requiresAuth: true },
beforeEnter: (to, from, next) => {
// Route-specific guard: validate ID is numeric
if (/^\d+$/.test(to.params.id)) {
next()
} else {
next('/404')
}
}
},
{
path: '/admin',
name: 'admin',
component: () => import('@/views/Admin.vue'),
meta: { requiresAuth: true, role: 'admin' }
},
{
path: '/:pathMatch(.*)*',
name: 'notFound',
component: NotFound
}
]
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes
})
// Global navigation guard
router.beforeEach((to, from, next) => {
const authStore = useAuthStore()
const isAuthenticated = authStore.isLoggedIn
// Check if route requires authentication
if (
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Vue.js Skills
Other Claude Code skills in the same category — free to download.
Vue Component
Create Vue 3 components with Composition API and props
Vue Pinia
Set up Pinia stores for state management in Vue
Vue Composable
Build reusable composables for Vue 3
Vue Nuxt
Scaffold Nuxt 3 app with SSR and auto-imports
Vue Testing
Write Vue component tests with Vitest and Testing Library
Vue TypeScript
Set up Vue 3 with TypeScript and type-safe props
Vue 3 Composables Pattern
Build reusable composables that share logic across Vue 3 components
Vue Pinia Global State
Manage global app state in Vue 3 with Pinia (modern Vuex replacement)
Want a Vue.js skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.