M
M
Maxim Zolotoy2019-04-26 12:01:05
Vue.js
Maxim Zolotoy, 2019-04-26 12:01:05

How to pass value from data to html attribute in vue?

I'm trying to do this (I think you understand the meaning of what I want to get) but it doesn't work) how is it customary to do such things?
<img src="/img/{{imgName}}.jpg" alt>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-04-26
@spacenear

Use dynamic value binding.
Collect the value itself using normal javascript expressions: or You can also make a computed property if imgName is a component property:
:src="`/img/${imgName}.jpg`"
:src="'/img/' + imgName + '.jpg'"

computed: {
  imgSrc() {
    return `/img/${this.imgName}.jpg`;
  },
},

:src="imgSrc"
Or make a method if you need to collect the path to the image in more than one place, with different names:
methods: {
  imgSrc: name => `/img/${name}.jpg`,
},

:src="imgSrc(imgName)"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question