1

I have a JS file with a function and component as below:

function emojify(name){
  var emj='';
  if (name!=''){
    emj= '<img src="/static/emojis/'+name+'.png" alt="img" > '
  }
  return emj
}


Vue.component("cmp2",{ props:["name"] , template: `<p>` + emojify("name") + `</p>`})

I call that component from HTML as below

<cmp2 name="ron" ></cmp2>

Image of ron doesnt show up. But if i declare my component as below it works:

Vue.component("cmp2",{ template:'<p>' + emojify("ron") + '</p>'})

So the image ron exists and works fine. I am just unable to pass value of props name to the function emojify. How can that be done?

1

1 Answer 1

2

You cannot do this as the template is computed at compilation time but props are evaluated at run / render time.

What you can do is bind the src attribute, eg

<p>
  <img v-if="name" :src="`/static/emojis/${name}.png`" alt="img">
</p>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.