DEV Community

Cover image for Stop Hardcoding Layouts in Vue — Use Route Meta Like a Pro
Kais
Kais

Posted on

Stop Hardcoding Layouts in Vue — Use Route Meta Like a Pro

If you're still switching layouts manually in your Vue app… you're doing it wrong.

Most people, especially beginners, end up writing messy logic just to handle different layouts like admin vs user. It works… but it scales like garbage.

Let me show you a cleaner, scalable way.


The Problem

You have multiple layouts:

  • Admin dashboard
  • User dashboard
  • Public pages

And you end up doing this:


<template>
<AdminLayout v-if="isAdmin">
<Dashboard />
</AdminLayout>
<UserLayout v-else>
<Dashboard />
</UserLayout>
</template>

This is bad.

Why?

  • Logic mixed with UI
  • Not reusable
  • Becomes a nightmare when app grows

The Right Way: Use Route Meta

Vue Router already gives you everything you need.

Define layouts directly in your routes:

const routes = [
{
path: "/admin",
component: () => import("@/pages/AdminDashboard.vue"),
meta: { layout: "admin", role: "admin" }
},
{
path: "/",
component: () => import("@/pages/Home.vue"),
meta: { layout: "default" }
}
];

Now your routes control:

  • Which layout to use
  • Who can access it

Clean and centralized.


Dynamic Layout System

Create a layout switcher in your main App.vue:
`

import { useRoute } from "vue-router";
import DefaultLayout from "@/layouts/DefaultLayout.vue";
import AdminLayout from "@/layouts/AdminLayout.vue";

const route = useRoute();

const layouts = {
default: DefaultLayout,
admin: AdminLayout
};








`
Boom.

Now layouts change automatically based on the route.

No more messy conditions.


Role-Based Access (Bonus)

You can also protect routes using navigation guards:
`
router.beforeEach((to, from, next) => {
const user = getUser(); // your auth logic

if (to.meta.role && user.role !== to.meta.role) {
return next("/unauthorized");
}

next();
});
`
Now your app:

  • Controls UI (layout)
  • Controls access (role)

All from the same place.


Why This Is Better

  • Scalable architecture
  • Clean separation of concerns
  • Easy to extend (add new layouts anytime)
  • Works perfectly with large apps

Go Further

If you want to level up:

  • Lazy load layouts
  • Add nested layouts
  • Support multiple roles (admin, editor, user)
  • Combine with state management (Pinia)

Final Thoughts

Vue doesn’t force you into a structure — but that’s also why many devs build messy apps.

Using route meta for layouts is one of those small decisions that makes a huge difference when your project grows.

If you're serious about building clean Vue apps… start doing this now.

Top comments (0)