Skip to content

Not getting dynamic loading working in Nuxt 3 #95

@mklueh

Description

@mklueh

I'm confused. What worked with Nuxt 2 does not work (or sometimes only when hot reload triggers the page refresh)

What I've tried so far:

1. Direct require in v-html

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="require(`~/assets/${icon}?raw`)"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
</script>

result:

404 Cannot find any route matching /assets/upload.svg?raw. (http://[::]:3000/nuxt_vite_node/module/../assets/upload.svg?raw)

2. Via await import (not according to the docs)

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")

let image = (await import("../assets/" + props.icon + "?raw")).default;

</script>

result:

404 Cannot find any route matching /assets/upload.svg?raw. (http://[::]:3000/nuxt_vite_node/module/../assets/upload.svg?raw)

However, the odd thing is, if I comment that line out let image = (await import("../assets/" + props.icon + "?raw")).default;
and let the page load, and then uncomment it, it will load the icon

3. Via import but not with await

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")

let image = import("../assets/" + props.icon + "?raw");

</script>

result: shows [OBJECT PROMISE]

4. With require in script tag

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")

let image = require("../assets/" + props.icon + "?raw");

</script>

result:

Cannot find module '../assets/upload.svg?raw' Require stack: - /my-project/src/atoms/BaseImage.vue

**5. With compute and require

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")


let image = computed(() => {
  return require("../assets/" + props.icon + "?raw");
})

</script>

result:

Cannot find module '../assets/upload.svg?raw' Require stack: - /my-project/src/atoms/BaseImage.vue

6. With compute and import and async

<template>
  <span class="base-icon text-brand" v-if="icon" v-html="image"/>
</template>

<script setup>

const props = defineProps({
  icon: {
    type: String
  }
})
const external = props.icon.startsWith("http")


let image = computed(async () => {
    return (await import("../assets/" + props.icon + "?raw")).default;
  }
)

</script>

result: shows [OBJECT PROMISE]

I'm not sure what else I could try and I don't see a reason why this happens.

Maybe I should add, that the component is not located in the actual Nuxt application, but a Nuxt library module, and the assets are located in the library as well.

I'd appreciate any help

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions