fix(nuxt3): child routes in vue-router don't have component field (#998)

Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
TenshiGoan 2021-10-20 21:49:15 +03:00 committed by GitHub
parent a3058eebae
commit c5920c21e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 2 deletions

View File

@ -6,6 +6,12 @@
<NuxtLink to="/about"> <NuxtLink to="/about">
About About
</NuxtLink> </NuxtLink>
<NuxtLink to="/parent">
Parent (index)
</NuxtLink>
<NuxtLink to="/parent/b">
Parent (b)
</NuxtLink>
<NuxtPage /> <NuxtPage />
</div> </div>

View File

@ -0,0 +1,6 @@
<template>
<div>
Parent
<RouterView />
</div>
</template>

View File

@ -0,0 +1,5 @@
<template>
<div>
Parent/b
</div>
</template>

View File

@ -0,0 +1,5 @@
<template>
<div>
Parent/index
</div>
</template>

View File

@ -2,7 +2,7 @@ import { existsSync } from 'fs'
import { defineNuxtModule, addTemplate, addPlugin } from '@nuxt/kit' import { defineNuxtModule, addTemplate, addPlugin } from '@nuxt/kit'
import { resolve } from 'pathe' import { resolve } from 'pathe'
import { distDir } from '../dirs' import { distDir } from '../dirs'
import { resolveLayouts, resolvePagesRoutes } from './utils' import { resolveLayouts, resolvePagesRoutes, addComponentToRoutes } from './utils'
export default defineNuxtModule({ export default defineNuxtModule({
name: 'router', name: 'router',
@ -40,7 +40,7 @@ export default defineNuxtModule({
filename: 'routes.mjs', filename: 'routes.mjs',
async getContents () { async getContents () {
const routes = await resolvePagesRoutes(nuxt) const routes = await resolvePagesRoutes(nuxt)
const serializedRoutes = routes.map(route => ({ ...route, component: `{() => import('${route.file}')}` })) const serializedRoutes = addComponentToRoutes(routes)
return `export default ${JSON.stringify(serializedRoutes, null, 2).replace(/"{(.+)}"/g, '$1')}` return `export default ${JSON.stringify(serializedRoutes, null, 2).replace(/"{(.+)}"/g, '$1')}`
} }
}) })

View File

@ -230,3 +230,11 @@ export async function resolveLayouts (nuxt: Nuxt) {
return { name, file } return { name, file }
}) })
} }
export function addComponentToRoutes (routes: NuxtRoute[]) {
return routes.map(route => ({
...route,
children: addComponentToRoutes(route.children),
component: `{() => import('${route.file}')}`
}))
}