Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How to make array with SVG's

I am trying to make an Icon component that loads in a js file that contains an array of objects with the svg values inside it.

My Icon component looks like this:

<template>
  <div class="icon"></div>
</template>

<script>
import icons from "~/assets/icons.js"

export default {
  mounted() {
    console.log(icons)
  }
}
</script>

My icons.js looks like this:

[
  {
    circle: `<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
      <circle id="Ellipse_1" data-name="Ellipse 1" cx="25" cy="25" r="25" fill="red"/>
    </svg>`,
  },
  {
    square: `<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
      <rect id="Rectangle_1" data-name="Rectangle 1" width="50" height="50" fill="#02f"/>
    </svg>`,
  },
  {
    triangle: `<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
      <path id="Polygon_1" data-name="Polygon 1" d="M25,0,50,50H0Z" fill="#00ff3c"/>
    </svg>`,
  },
]

When I try to read the icons with the console.log it returns an empty object. I've tried changing my icons.js to see if it was because of these things: ``

But even when I change it to this it returns an empty object:

[
  {name: "foo"},
  {name: "bar"}
]

My project has been newly made and has no other content besides this, could it be possible i selected a wrong setting while creating the nuxt project? Or am I looking at this all wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to export your data. BTW, I don't see any reason for using an array in your case. It seems like your data should be an object like this:

export default {
    circle: `<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
      <circle id="Ellipse_1" data-name="Ellipse 1" cx="25" cy="25" r="25" fill="red"/>
    </svg>`,
    square: `<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
      <rect id="Rectangle_1" data-name="Rectangle 1" width="50" height="50" fill="#02f"/>
    </svg>`,
}
//Then import like
import icons from "~/assets/icons.js"
// use it like
icons.circle
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda