Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load the SVG into a dynamic component #75

Open
kissu opened this issue Nov 4, 2020 · 1 comment
Open

Load the SVG into a dynamic component #75

kissu opened this issue Nov 4, 2020 · 1 comment

Comments

@kissu
Copy link

kissu commented Nov 4, 2020

Hi ! 👋🏻
I was wondering if that kind of code may work <component is="svg-grey-home"></component>.

Because we have Nuxt components now, so maybe it is possible in any way.

Without having to use

<script>
export default {
  components: {
    SvgGreyHome: () => import('~/assets/svg/grey/home.svg'),
  },
}
</script>

In my case, writting <svg-grey-home></svg-grey-home> totally works (the setup is done for that) but I want to make it dynamic without the need to import it beforehand.

@amirhoseinsalimi
Copy link

What you're looking for is completely doable.

This is from a project that I'm currently working on:

...
components: {
  VideoIcon: () => import('~/assets/icons/video.svg'),
  CalendarIcon: () => import('~/assets/icons/calendar.svg'),
},
...
computed: {
  iconComponentName() {
    if (!this.icon) {
      return;
    }

    return `${this.capitalize(this.icon)}Icon`;
  },
},

That this.icon is a prop. Then in your template, you can write:

<template v-if="icon">
  <Component :is="iconComponentName" />
</template>

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants