1đź‘Ť
What version of vue.js are you using?
In docs it says that: “Filters are usable in two places: mustache interpolations and v-bind expressions (the latter supported in 2.1.0+).”
So you can’t use them in v-for directive. You can use computed instead, like this:
<div class="container">
<ul>
<li v-for="item in myFilteredList">
{{ item.name }}
</li>
</ul>
</div>
new Vue({
el: '.container',
computed: {
myFilteredList: function() {
return this.myList.filter(function(item) {
return item.value >= 2;
});
}
},
data: {
message: 'Hello world',
myList: [
{
name: 'item1',
value: 1
},
{
name: 'item2',
value: 2
},
{
name: 'item3',
value: 3
}
]
}
});
Link to jsfiddle: https://jsfiddle.net/nryzwamm/.
However, ideally component should accept already filtered data passed to it from the outside. This will make your components more reusable, because they will contain less specific logic, like filtration, which may not be required in some other case.
1đź‘Ť
Vue filters are only supposed to be used for re-processing text in interpolations or v-bind
attributes. I think you’re best off creating a computed property and looping over that in your v-for
.