How can we dynamically import svg files?
See original GitHub issueI am not sure if this loader supports this:
computed: {
currentIcon() {
return () => import('path/to/svg/file.svg');
}
}
and then we can use as
<template>
<compoment
:is="currentIcon"
:aria-hidden="hidden"
/>
</template>
Currently console output as
runtime-core.esm-bundler.js:38 [Vue warn]: Invalid VNode type: undefined (undefined)
at <Anonymous is=fn aria-hidden=true >
at <UiIcon name="notification" >
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
Top Results From Across the Web
How to dynamically import SVG and render it inline
I want to dynamically import that svg based on the name passed to the function. It looks like this: import React from 'react';...
Read more >React, dynamically importing SVG's | by Eric Khoury - Medium
Import all SVG's from a folder at runtime, i.e based on props; Render the SVG as a React component, as in <MySvg />....
Read more >Using React.lazy() to dynamically import svg files as ... - Reddit
My attempt in making that simpler is the following component with all svg files in a different folder: import React, { Suspense }...
Read more >react-dynamic-svg-import - CodeSandbox
react-dynamic-svg-import. 1. Embed Fork ... Files. public. src. App.js. index.js. svg1.svg. svg2.svg. package.json. Dependencies. react. 16.12.0.
Read more >Dynamic SVG component in Vite + React + TS
import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import svgr from "vite-plugin-svgr"; // transform svg to react ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For those wanting to use
<script setup>
you can do this. This worked for me on Nuxt3.Hello! I was trying to do something similar and after several ideas that occurred to me, I managed to do it this way:
assets/icons
all the svg that you will use. Create a fileindex.js
and import and export the svgsrc/components
, create componentIcon.vue
:IMPORTANT NOTE: If your
svg
has a name inkebab-case
(i. e"heart-pulse.svg"
), when you use theicon
component, the prop name must be insnake_case
(i. e"heart_pulse"
)