0

using Vite I need to import image url for example with the way like this

 <svg :src="import('®/@material-icons/svg/1k/outline.svg')"></svg>

but instead of url I'm getting src="[object Promise]" is there any ways to make it work correctly?

2 Answers 2

3

You have to use require

<svg :src="require('®/@material-icons/svg/1k/outline.svg')"></svg>

Ensure to have the correct path for the file

Fore Vite

<svg :src="svgPath"></svg>

<script>
  async created(){
    this.svgPath = (await import("®/@material-icons/svg/1k/outline.svg")).default()
  }
  data(){
    return {svgPath:""}
  }
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

but require is not working with Vite
Edited. Please try. I came across with this solution
thank you, but seems vue render works synchronously and I cannot use await( I'm getting 'await' is only allowed within async functions and at the top levels of modules. and I don't know is there any ways to change templates render type to async
I've updated the answer, give it a try
You can make an utility function in a separate file, and pass the path as parameter and return the default value
|
1

The easiest way to work with images in Vitejs is to place images inside the public folder then use them without any import, Assume that we have a folder named svg inside the public one :

<svg src="/svg/1k/outline.svg"></svg>

Or import it as module then bind it to the src attribute :

<template>
  <svg :src="imgUrl"></svg>
</template>
<script setup>
   import imgUrl from '®/@material-icons/svg/1k/outline.svg'
</script>

1 Comment

thanks but I'm using @material-icons/svg which are located in node_modules seems the only problem I have is I have 30+ svgs and that's pretty hard to remember name of every variable using import every time in script((

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.