Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

296
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda