Gathering detailed insights and metrics for vite-plugin-vue-layouts-next
Gathering detailed insights and metrics for vite-plugin-vue-layouts-next
Gathering detailed insights and metrics for vite-plugin-vue-layouts-next
Gathering detailed insights and metrics for vite-plugin-vue-layouts-next
Vue layout plugin for Vite, supports the latest versions
npm install vite-plugin-vue-layouts-next
Typescript
Module System
Node Version
NPM Version
TypeScript (99.33%)
JavaScript (0.67%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
34 Stars
229 Commits
2 Forks
1 Branches
21 Contributors
Updated on Jul 11, 2025
Latest Version
1.0.0
Package Id
vite-plugin-vue-layouts-next@1.0.0
Unpacked Size
22.93 kB
Size
7.48 kB
File Count
6
NPM Version
10.9.0
Node Version
22.11.0
Published on
Jul 01, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
Router based layout for Vue 3 applications using Vite
A fork of vite-plugin-vue-layouts with some improvements and fixes, supports the latest versions of Vite and Vue.
This works best along with the vite-plugin-pages.
Layouts are stored in the /src/layouts
folder by default and are standard Vue components with a <router-view></router-view>
in the template.
Pages without a layout specified use default.vue
for their layout.
You can use route blocks to allow each page to determine its layout. The block below in a page will look for /src/layouts/users.vue
for its layout.
1<route lang="yaml"> 2meta: 3 layout: users 4</route>
Install Layouts:
1# npm 2npm install -D vite-plugin-vue-layouts-next 3 4# yarn 5yarn add -D vite-plugin-vue-layouts-next 6 7# pnpm 8pnpm add -D vite-plugin-vue-layouts-next
Add to your vite.config.ts
:
1import Vue from '@vitejs/plugin-vue' 2import Pages from 'vite-plugin-pages' 3import Layouts from 'vite-plugin-vue-layouts-next' 4import { defineConfig } from 'vite' 5 6export default defineConfig({ 7 plugins: [Vue(), Pages(), Layouts()], 8})
In main.ts, you need to add a few lines to import the generated code and setup the layouts.
1import { setupLayouts } from 'virtual:generated-layouts' 2import { createRouter } from 'vue-router' 3import generatedRoutes from '~pages' 4 5const routes = setupLayouts(generatedRoutes) 6 7const router = createRouter({ 8 // ... 9 routes, 10})
1import { setupLayouts } from 'virtual:generated-layouts' 2import { createRouter } from 'vue-router' 3import { routes } from 'vue-router/auto-routes' 4 5const router = createRouter({ 6 // ... 7 routes: setupLayouts(routes), 8})
If you want type definition of virtual:generated-layouts
, add vite-plugin-vue-layouts-next/client
to compilerOptions.types
of your tsconfig
:
1{ 2 "compilerOptions": { 3 "types": ["vite-plugin-vue-layouts-next/client"] 4 } 5}
1interface UserOptions { 2 layoutsDirs?: string | string[] 3 pagesDirs?: string | string[] | null 4 extensions?: string[] 5 exclude?: string[] 6 defaultLayout?: string 7 importMode?: (name: string) => 'sync' | 'async' 8}
To use custom configuration, pass your options to Layouts when instantiating the plugin:
1// vite.config.ts 2import Layouts from 'vite-plugin-vue-layouts-next' 3import { defineConfig } from 'vite' 4 5export default defineConfig({ 6 plugins: [ 7 Layouts({ 8 layoutsDirs: 'src/mylayouts', 9 pagesDirs: 'src/pages', 10 defaultLayout: 'myDefault' 11 }), 12 ], 13})
Relative path to the layouts directory. Supports globs. All .vue files in this folder are imported async into the generated code.
Can also be an array of layout dirs
Can use **
to support scenarios like module1/layouts
and modules2/layouts
with a setting of src/**/layouts
Any files named __*__.vue
will be excluded, and you can specify any additional exclusions with the exclude
option
Default: 'src/layouts'
Defines the pages dir to avoid HMR reloading for all added or deleted files anywhere in the project.
Relative path to the pages directory. If you want it to watch for all files, like in v0.8.0 or earlier, set to null.
Can also be an array of layout dirs or use **
glob patterns
Default: 'src/pages'
Valid file extensions for page components.
Default: ['vue']
List of path globs to exclude when resolving pages.
Filename of default layout (".vue" is not needed).
Default: 'default'
Mode for importing layouts.
Default: ssg is 'sync'
,other is 'async'
setupLayouts
transforms the original router
by
children
property.Simply put, layouts are nested routes with the same path.
Before:
1router: [ page1, page2, page3 ]
After setupLayouts()
:
1router: [ 2 layoutA: page1, 3 layoutB: page2, 4 layoutA: page3, 5]
That means you have the full flexibility of the vue-router API at your disposal.
Layouts and Transitions work as expected and explained in the vue-router docs only as long as Component
changes on each route. So if you want a transition between pages with the same layout and a different layout, you have to mutate :key
on <component>
(for a detailed example, see the vue docs about transitions between elements).
App.vue
1<template> 2 <router-view v-slot="{ Component, route }"> 3 <transition name="slide"> 4 <component :is="Component" :key="route" /> 5 </transition> 6 </router-view> 7</template>
Now Vue will always trigger a transition if you change the route.
If you want to send data down from the layout to the page, use props
1<router-view foo="bar" />
If you want to set state in your page and do something with it in your layout, add additional properties to a route's meta
property. Doing so only works if you know the state at build-time.
You can use the <route>
block if you work with vite-plugin-pages.
In page.vue
:
1<template><div>Content</div></template> 2<route lang="yaml"> 3meta: 4 layout: default 5 bgColor: yellow 6</route>
Now you can read bgColor
in layout.vue
:
1<script setup lang="ts"> 2import { useRouter } from 'vue-router' 3</script> 4<template> 5 <div :style="`background: ${useRouter().currentRoute.value.meta.bgColor};`"> 6 <router-view /> 7 </div> 8</template>
If you need to set bgColor
dynamically at run-time, you can use custom events.
Emit the event in page.vue
:
1<script setup lang="ts"> 2import { defineEmit } from 'vue' 3const emit = defineEmit(['setColor']) 4 5if (2 + 2 === 4) 6 emit('setColor', 'green') 7else 8 emit('setColor', 'red') 9</script>
Listen for setColor
custom-event in layout.vue
:
1<script setup lang="ts"> 2import { ref } from 'vue' 3 4const bgColor = ref('yellow') 5const setBg = (color) => { 6 bgColor.value = color 7} 8</script> 9 10<template> 11 <main :style="`background: ${bgColor};`"> 12 <router-view @set-color="setBg" /> 13 </main> 14</template>
The clientSideLayout uses a simpler virtual file + glob import scheme, This means that its hmr is faster and more accurate, but also more limited
1// vite.config.ts
2import { ClientSideLayout } from 'vite-plugin-vue-layouts-next'
3import { defineConfig } from 'vite'
4
5export default defineConfig({
6 plugins: [
7 ClientSideLayout({
8 layoutsDir: 'src/mylayouts', // default to 'src/layouts'
9 defaultLayout: 'myDefault', // default to 'default', no need '.vue'
10 importMode: 'sync' // The default will automatically detect -> ssg is sync,other is async
11 }),
12 ],
13})
No vulnerabilities found.
No security vulnerabilities found.