Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!