Vue Router Layout Selector

0.4.1 · active · verified Sun Apr 19

Vue Router Layout is a lightweight utility designed for dynamically resolving layout components within Vue Router applications. Currently stable at version 0.4.1, the library provides a programmatic way to define layouts for routes by using a factory function, `createRouterLayout`, which then generates a `<RouterLayout>` component. This component is subsequently integrated into Vue Router's `routes` configuration. The package supports dynamic imports for layout components, enabling efficient code splitting. A key differentiator is its focus on minimalist layout resolution, allowing developers to specify layouts directly within their page components via a `layout` option, including passing props since v0.4.0. Its release cadence is irregular but consistent with bug fixes and features. Crucially, version 0.2.0 marked a breaking change by dropping Vue 2 support in favor of Vue 3, necessitating version awareness for project compatibility. For more comprehensive routing solutions, the author points to `vue-cli-plugin-auto-routing`.

Common errors

Warnings

Install

Imports

Quickstart

Initializes a `RouterLayout` component, configuring Vue Router to use it for dynamic layout resolution based on route meta or component options, demonstrating basic layout switching and prop passing.

import { createApp } from 'vue';
import { createRouter, createWebHistory } from 'vue-router';
import { createRouterLayout } from 'vue-router-layout';

// Define your layout components
const DefaultLayout = { template: '<div><h1>Default Layout</h1><router-view /></div>' };
const AdminLayout = { template: '<div><h2>Admin Panel Layout</h2><router-view /></div>' };

// Create <RouterLayout> component.
const RouterLayout = createRouterLayout(layout => {
  if (layout === 'admin') {
    return Promise.resolve(AdminLayout);
  }
  return Promise.resolve(DefaultLayout);
});

const routes = [
  {
    path: '/',
    component: RouterLayout,
    children: [
      {
        path: '',
        component: { template: '<p>Home Page</p>', layout: 'default' },
      },
      {
        path: 'dashboard',
        component: { template: '<p>Admin Dashboard</p>', layout: { name: 'admin', props: { title: 'Admin' } } },
      },
      {
        path: 'profile',
        component: { template: '<p>User Profile</p>', layout: 'default' },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

const app = createApp({});
app.use(router);
app.mount('#app');

// For a real application, you would render this in an HTML file:
// <div id="app"></div>
// <router-view></router-view>

view raw JSON →