Skip to content

Commit

Permalink
feat: alternative getRoutes that filters layouts
Browse files Browse the repository at this point in the history
closes #55
  • Loading branch information
JohnCampionJr committed Dec 5, 2023
1 parent 1041502 commit 31d3474
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare module 'layouts-generated' {
}

declare module 'virtual:generated-layouts' {
import type { RouteRecordRaw } from 'vue-router'
import type { Router, RouteRecordRaw } from 'vue-router'
// need any here due to different types for vue-router versions
export function createGetRoutes(router: Router | any, withLayout?: boolean): () => RouteRecordRaw[]
export function setupLayouts(routes: RouteRecordRaw[]): RouteRecordRaw[]
}
4 changes: 3 additions & 1 deletion examples/client-side/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { setupLayouts } from 'virtual:generated-layouts'
import { createGetRoutes, setupLayouts } from 'virtual:generated-layouts'
import generatedRoutes from 'virtual:generated-pages'
import App from './App.vue'
import './index.css'
Expand All @@ -12,6 +12,8 @@ const router = createRouter({
routes,
})

const getRoutes = createGetRoutes(router)
console.log(getRoutes())
const app = createApp(App)

app.use(router)
Expand Down
5 changes: 4 additions & 1 deletion examples/spa/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { setupLayouts } from 'virtual:generated-layouts'
import { createGetRoutes, setupLayouts } from 'virtual:generated-layouts'
import generatedRoutes from 'virtual:generated-pages'
import App from './App.vue'
import './index.css'
Expand All @@ -12,6 +12,9 @@ const router = createRouter({
routes,
})

const getRoutes = createGetRoutes(router)
console.log(getRoutes())

const app = createApp(App)

app.use(router)
Expand Down
12 changes: 11 additions & 1 deletion src/RouteLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { ResolvedOptions } from './types'
function getClientCode(importCode: string, options: ResolvedOptions) {
const code = `
${importCode}
export const createGetRoutes = (router, withLayout = false) => {
const routes = router.getRoutes()
if (withLayout) {
return routes
}
return () => routes.filter(route => !route.meta.isLayout)
}
export function setupLayouts(routes) {
return routes.map(route => {
Expand All @@ -14,7 +21,10 @@ export function setupLayouts(routes) {
return {
path: route.path,
component: layouts[componentName],
children: route.path === '/' ? [route] : [{...route, path: ''}]
children: route.path === '/' ? [route] : [{...route, path: ''}],
meta: {
isLayout: true
}
}
}
})
Expand Down

0 comments on commit 31d3474

Please sign in to comment.