admin管理员组文章数量:1315079
I'm just getting started with Vue.js; impressed by what it can do, but having trouble parsing the basics out of the documentation.
Assuming some checkboxes or a select[multiple]:
<label><input v-model="fruits" type="checkbox" value="apple"> apple</label><br>
<label><input v-model="fruits" type="checkbox" value="orange"> orange</label><br>
<label><input v-model="fruits" type="checkbox" value="banana"> banana</label><br>
Bound to an array in a Vue model:
var vm = new Vue({
el: '#foo',
data: {
fruits: ['orange'],
},
});
I want to manipulate the classes on some other elements based on what is or isn't in the vm.$data.fruits
array. I can't figure out the syntax to use with v-bind:class
to check within the fruits
array. I'm guessing it's something like this:
<div id="myfruits" v-bind:class="{ 'apple': fruits.apple, 'banana': fruits.banana, 'orange': fruits.orange }">fruits</div>
<div id="apple" v-bind:class="{ 'banana': fruits.banana }">You've got bananas!</div>
I'm sure there must be a way to do this inArray
kind of logic with v-bind
. If not, can I refer to a method instead of a value? Like v-bind:class="{ 'orange': hasOranges() }"
?
I'm just getting started with Vue.js; impressed by what it can do, but having trouble parsing the basics out of the documentation.
Assuming some checkboxes or a select[multiple]:
<label><input v-model="fruits" type="checkbox" value="apple"> apple</label><br>
<label><input v-model="fruits" type="checkbox" value="orange"> orange</label><br>
<label><input v-model="fruits" type="checkbox" value="banana"> banana</label><br>
Bound to an array in a Vue model:
var vm = new Vue({
el: '#foo',
data: {
fruits: ['orange'],
},
});
I want to manipulate the classes on some other elements based on what is or isn't in the vm.$data.fruits
array. I can't figure out the syntax to use with v-bind:class
to check within the fruits
array. I'm guessing it's something like this:
<div id="myfruits" v-bind:class="{ 'apple': fruits.apple, 'banana': fruits.banana, 'orange': fruits.orange }">fruits</div>
<div id="apple" v-bind:class="{ 'banana': fruits.banana }">You've got bananas!</div>
I'm sure there must be a way to do this inArray
kind of logic with v-bind
. If not, can I refer to a method instead of a value? Like v-bind:class="{ 'orange': hasOranges() }"
?
2 Answers
Reset to default 3Just to build on Linus's answer, there are a couple other ways you can handle this.
You can put the check in your binding expression:
<div id="apple" v-bind:class="{ 'banana': fruits.indexOf('banana') > -1 }">You've got bananas!</div>
If, as in one of your examples, the class names match the fruit values bound to your checkboxes, you can bind directly to the fruits
array:
<div id="myfruits" v-bind:class="fruits">fruits</div>
Or, if your class names are different than the fruits, you can bind to a puted property:
<div id="myfruits" v-bind:class="classes">fruits</div>
new Vue({
el: '#app',
data: {
fruits: ['orange'],
},
puted: {
classes: function() {
return {
'has-banana': this.fruits.indexOf('banana') > -1,
'has-apple': this.fruits.indexOf('apple') > -1,
'has-orange': this.fruits.indexOf('orange') > -1
};
}
}
})
create a method that checks the fruits
array with #indexOf()
weither it contains the fruit.
var vm = new Vue({
el: '#foo',
data: {
fruits: ['orange'],
},
methods: {
hasFruit(fruit) {
return this.fruits.indexOf(fruit) === -1 ? false : true
}
}
});
<div id="myfruits" v-bind:class="{ 'apple': hasFruit('apple'), 'banana': hasFruit('banana'), 'orange': hasFruit('orange') }">
fruits
</div>
本文标签: javascriptVuejs vbind to a value within an arrayStack Overflow
版权声明:本文标题:javascript - Vue.js v-bind to a value within an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971880a2407889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论