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

303
Visualizações
Displaying image (in HTML) from a computed property in Vue JS

I would like to display an svg icon generated from a computed property. When I use the VueJS chrome extension, I can see that the prop is being populated correctly with the link to the svg icon. However, the HTML code still does not render the icon on the page. What am I missing in the syntax?

Here is my HTML(in Vue.JS):

var markUp = Vue.compile('\
     <a class="box" ref="addingBox" href="#" role="button" :id="\'Add_\' + type" @click.prevent="clickBox" v-preventTabbing:[preventTabbingValue]>\
     <div class="ab-Grid">\
       <div class="ab-Grid-row">\
          <div class="ab-Grid-col" :class="{ \'ab-sm2\': this.type != \'showMoreOptions\' }">\
            <div v-if="type != \'hasMatchingKeys\'" class="box-icon" aria-hidden="true" :class="icon"></div>\
            <img v-else :src="require(keysIcon)" role="presentation" class="box-icon-img"/>\
          </div>\
          <div class="ab-Grid-col ab-sm10">\
          <div class="box-title">{{ title }}</div>\
          <div class="box-description" :class="{ \'text-align-center\': this.type == \'showMoreOptions\' }">{{ desc }}</div>\
       </div>\
    </div>\
  </div>\
</a>');

Here is my implementation of the computed property:

computed: {
  hasMatchingKeys() {
    return ["a", "b"].includes(this.type)
  },
  keysIcon() {
    return new Map([['a', this.aIcon], ['b', this.bIcon]]).get(this.type) || ""
  }
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try replacing :src="require(keysIcon)" with :src="keysIcon".

Working example:

const res = Vue.compile('<div>\
    <input type="radio" id="one" value="a" v-model="type"> Blue<br>\
    <input type="radio" id="two" value="b" v-model="type"> Green<br>\
    <img :src="keysIcon" /></div>');

const demo = new Vue({
  el: "#demo",
  data: {
    type: "a",
    aIcon: "https://i.stack.imgur.com/gJmeJ.png",
    bIcon: "https://i.stack.imgur.com/T5uTa.png",
  },
  render: res.render,
  staticRenderFns: res.staticRenderFns,
  computed: {
    keysIcon() {
      return (
        new Map([
          ["a", this.aIcon],
          ["b", this.bIcon],
        ]).get(this.type) || ""
      );
    },
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo"></div>

about 4 years ago · Santiago Trujillo 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