question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can we dynamically import svg files?

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
JacobRexcommented, Aug 24, 2022

For those wanting to use <script setup> you can do this. This worked for me on Nuxt3.

<template>
  <component :is="icon"/>
</template>

<script lang="ts" setup>
const props = defineProps({
  name: {
    type: String,
    required: true
  },
});

const icon = defineAsyncComponent(() => import(`../../assets/icons/${props.name}.svg`));
</script>
2reactions
jrafaaaelcommented, Jul 7, 2021

Hello! I was trying to do something similar and after several ideas that occurred to me, I managed to do it this way:

  1. Put in assets/icons all the svg that you will use. Create a file index.js and import and export the svg
// src/assets/icons/index.js
import add from './add.svg'
import multiply from './multiply.svg'
// etc

export {
  add,
  multiply,
  // etc
}
  1. In path src/components, create component Icon.vue:
// src/components/Icon.vue
<template>
  <component :is="icon"/>
</template>

<script>
import * as icons from "../assets/icons";

export default {
  name: "Icon",
  props: ["name"],
  computed: {
    icon() {
      return icons[this.name]
    }
  },
};
</script>
  1. Use it!
<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld msg="Hello Vue 3 + Vite" />
  <icon name="add" />
  <icon name="multiply" />
</template>

<script setup>
import HelloWorld from './components/HelloWorld.vue'
import Icon from "./components/Icon.vue";
</script>

IMPORTANT NOTE: If your svg has a name in kebab-case (i. e "heart-pulse.svg"), when you use the icon component, the prop name must be in snake_case (i. e "heart_pulse")

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found