How to generated virtual route #368
-
|
I did it! Thanks @posva Code for unplugin-vue-router// vite.config.ts
import { type EditableTreeNode } from 'unplugin-vue-router'
import VueRouter from 'unplugin-vue-router/vite'
// ...
const locales = ['zh', 'en']
const makeRoute = (
route: EditableTreeNode,
parent: EditableTreeNode,
locale: string,
path: string,
file?: string,
) => {
//
const newRoute = parent.insert(path, file as any)
if (file == null) newRoute.components.clear()
// copy meta data
newRoute.addToMeta(route.meta)
newRoute.addToMeta({ locale })
// process children
route.children.forEach(subRoute => {
const newSubRoute = makeRoute(
subRoute,
newRoute,
locale,
subRoute.path,
subRoute.components.get('default'),
)
})
return newRoute
}
// ...
VueRouter({
beforeWriteFiles: async root => {
routes.children.forEach(route => {
const file = route.components.get('default')
const path = route.path
// make i18n route
locales.forEach(locale => {
const newRoute = makeRoute(
route,
root,
locale,
`/${locale}${path}`,
file,
)
})
route.delete()
})
},
}),
// ...Origin:Hi, I used ViteSSG and such as I have component file like When I using Code for vite-plugin-pagesimport Pages from 'vite-plugin-pages'
const locales = ['zh', 'en']
// ...
Pages({
// ...
onRoutesGenerated: routes =>
routes.flatMap(i =>
locales.map(locale => {
const route = structuredClone(i)
route.path = `/${locale}${route.path}`
const setMeta = (route: any) => {
route.name = `${route.name} - ${locale}`
route.meta ??= {}
route.meta.locale = locale
if (Array.isArray(route.children))
route.children.forEach(setMeta)
return route
}
return setMeta(route)
}),
),
}),
// ...I found |
Beta Was this translation helpful? Give feedback.
Answered by
posva
Apr 22, 2024
Replies: 1 comment
-
|
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
frg2089
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
beforeWriteFilesshould be the way.EditableTreeNodehas acomponentsMap:unplugin-vue-router/src/core/extendRoutes.ts
Line 71 in 76eb565