0👍
You can do that, but need to use a different v-bind
syntax.
You pass an object with keys being the HTML attribute name.
You can generate an object at runtime using Computed property names
for object initialization
Try this
<ul>
<li v-for="item in list" v-bind="{[`${item.directive}`]: ''}">{{item.name}}</li>
</ul>
- [Vuejs]-Odd behavior for array[value]
- [Vuejs]-How to load data based on id using Vue js and Laravel?
0👍
I think the preferred way to achieve something like this is the following:
new Vue({
el: '#app',
data: {
list: [
{ name: "test-a", directive: "v-testa" },
{ name: "test-b", directive: "v-testb" },
{ name: "test-c", directive: "v-testc" }
]
},
methods: {
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>List:</h2>
<ol>
<li v-for="item in list" :data-directive="item.directive">
{{ item.name }}
</li>
</ol>
</div>
Because if you start making up your own attributes, you run into two problems:
- An HTML validator will assume that your attribute is incorrect
- You are competing with other code which is also making up its own attributes
The data- attribute prefix has two benefits:
- HTML validators will ignore the attribute when doing validation
- JavaScript will gather these attributes into the special
data
object to provide easy access
So using the data- prefix allows you to work with an otherwise invalid attribute by telling the validator to ignore it.
- [Vuejs]-Creating a search bar using Laravel and Vue JS (Vuetify Framework) – resulting in error 'Cannot read property 'protocol' of undefined'
- [Vuejs]-Axios Flask. Parsing fileblob and json
Source:stackexchange.com