Gathering detailed insights and metrics for vite-plugin-vue-layouts
Gathering detailed insights and metrics for vite-plugin-vue-layouts
Gathering detailed insights and metrics for vite-plugin-vue-layouts
Gathering detailed insights and metrics for vite-plugin-vue-layouts
npm install vite-plugin-vue-layouts
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
478 Stars
133 Commits
66 Forks
4 Watching
2 Branches
19 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (99.23%)
JavaScript (0.77%)
Cumulative downloads
Total Downloads
Last day
7.5%
7,606
Compared to previous day
Last week
4.8%
39,252
Compared to previous week
Last month
-6.9%
162,212
Compared to previous month
Last year
56.3%
1,734,998
Compared to previous year
3
Router based layout for Vue 3 applications using Vite
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.
See the Vitesse starter template for a working example.
1<route lang="yaml"> 2meta: 3 layout: users 4</route>
Install Layouts:
1$ npm install -D vite-plugin-vue-layouts
Add to your vite.config.js
:
1import Vue from '@vitejs/plugin-vue'; 2import Pages from 'vite-plugin-pages'; 3import Layouts from 'vite-plugin-vue-layouts'; 4 5export default { 6 plugins: [Vue(), Pages(), Layouts()], 7};
In main.ts, you need to add a few lines to import the generated code and setup the layouts.
1import { createRouter } from 'vue-router' 2import { setupLayouts } from 'virtual:generated-layouts' 3import generatedRoutes from '~pages' 4 5const routes = setupLayouts(generatedRoutes) 6 7const router = createRouter({ 8 // ... 9 routes, 10});
1import { createRouter } from 'vue-router/auto' 2import { setupLayouts } from 'virtual:generated-layouts' 3 4const router = createRouter({ 5 // ... 6 extendRoutes: (routes) => setupLayouts(routes), 7})
If you want type definition of virtual:generated-layouts
, add vite-plugin-vue-layouts/client
to compilerOptions.types
of your tsconfig
:
1{ 2 "compilerOptions": { 3 "types": ["vite-plugin-vue-layouts/client"] 4 } 5}
1interface UserOptions { 2 layoutsDirs?: string | string[] 3 pagesDirs?: string | string[] | null 4 exclude: string[] 5 defaultLayout?: string 6}
To use custom configuration, pass your options to Layouts when instantiating the plugin:
1// vite.config.js 2import Layouts from 'vite-plugin-vue-layouts'; 3 4export default { 5 plugins: [ 6 Layouts({ 7 layoutsDirs: 'src/mylayouts', 8 pagesDirs: 'src/pages', 9 defaultLayout: 'myDefault' 10 }), 11 ], 12};
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'
setupLayouts
transforms the original router
by
children
property.Simply put, layouts are nested routes with the same path.
Before:
router: [ page1, page2, page3 ]
After setupLayouts()
:
router: [
layoutA: page1,
layoutB: page2,
layoutA: page3,
]
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
<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> 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> 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> 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.js 2import { ClientSideLayout } from 'vite-plugin-vue-layouts' 3 4export default { 5 plugins: [ 6 ClientSideLayout({ 7 layoutsDir: 'src/mylayouts', // default to 'src/layouts' 8 defaultLayout: 'myDefault', // default to 'default', no need '.vue' 9 importMode: 'sync' // The default will automatically detect -> ssg is sync,other is async 10 }), 11 ], 12};
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/29 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
29 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More