Stack
- Vue 3.2
- Nuxt 3.0.0-rc.4
- Vite 2.9
Goal
Trying to dynamically load a component with <component :is /> from a variable instead of a static string.
Code
Here is a minified example of what I am trying to achieve:
<script lang="ts" setup>
import { defineAsyncComponent } from 'vue'
const filepathStatic = '../foobar/Foobar.vue'
const bar = 'bar'
const filepathDynamic = `../foobar/Foo${bar}.vue` // meets all requirements
const asyncComponent = defineAsyncComponent(() => {
return import('../foobar/Foobar.vue') // WORKS
return import(filepathStatic) // does NOT work
return import(filepathDynamic) // does NOT work
})
</script>
<template>
<div>
<component :is="asyncComponent" />
</div>
</template>
Errors
1.) Vite-Error
The above dynamic import cannot be analyzed by vite.
See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.
=> The dynamic filenames meet ALL the requirements stated on this page.
2.) Nuxt-Error
500
Cannot read properties of undefined (reading 'stubModule')
at __instantiateModule__ (./.nuxt/dist/server/server.mjs:4094:11)
at __ssrLoadModule__ (./.nuxt/dist/server/server.mjs:4085:25)
at ssrImport (./.nuxt/dist/server/server.mjs:4110:13)
at ssrDynamicImport (./.nuxt/dist/server/server.mjs:4121:12)
at ./.nuxt/dist/server/server.mjs:2808:14
at load (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:2255:17)
at setup (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:2308:24)
at callWithErrorHandling (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:157:22)
at setupStatefulComponent (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7084:29)
at setupComponent (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7039:11)
=> I have NO idea, why this does not work.