Answer the question
In order to leave comments, you need to log in
How to specify the name of a variable from data in a template in a loop?
<div v-for="(elem, key) in data" :="key">{{ elem }}</div
export default {
data() {
return {
a: 1,
b: "some",
c: [1, 2, 3],
data: ['a', 'b', 'c']
}
}
}
Answer the question
In order to leave comments, you need to log in
<div
v-for="(elem, key) in data"
:key="key"
>
{{ $data[elem] }}
</div>
At a glance, I see two options:
1. wrap your variables a, b, c in an object:
export default {
data() {
return {
myVars: {
a: 1,
b: "some",
c: [1, 2, 3],
},
data: ['a', 'b', 'c'],
}
}
}
<div v-for="(elem, key) in data" :="key">{{ myVars[elem] }}</div>
export default {
data() {
return {
a: 1,
b: "some",
c: [1, 2, 3],
data: ['a', 'b', 'c'],
}
},
methods: {
getDataValue(param) {
return this[param] || null;
},
},
}
<div v-for="(elem, key) in data" :="key">{{ getDataValue(elem) }}</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question