[英]How to filter items in Vue 3?
In Vue 2, we filter items conveniently just using |
在 Vue 2 中,我们只使用|
就可以方便地过滤项目。 and filters
.和filters
。 But it isn't in Vue 3.但它不在 Vue 3 中。
As we know,we can just use "computed" to change a value to another.正如我们所知,我们可以只使用“计算”将一个值更改为另一个值。
But how can I change values of an array?但是如何更改数组的值?
Vue 2 Vue 2
<template>
<ul>
<li v-for="(index,value) in array1" :key="index">
{{ value | valuefilter}}
</li>
</ul>
</template>
<script>
...
export {
...
filters:{
valuefilter(value){
return '$'+ value
}
}
...
}
</script>
Filters are removed from Vue 3.0 and no longer supported过滤器从 Vue 3.0 中移除并且不再支持
Here is the way you can use the filter in Vue 3这是在 Vue 3 中使用过滤器的方法
click the link below:点击下面的链接:
https://v3.vuejs.org/guide/migration/filters.html#overview https://v3.vuejs.org/guide/migration/filters.html#overview
Use a computed to filter the data beforehand to the way you want it, then iterate over that computed instead of the raw data.使用计算来预先过滤数据到你想要的方式,然后迭代计算而不是原始数据。
This is essentially what a filter would do, but with the advantage of keeping the template a little clearer from the logic in the component:这本质上是过滤器所做的,但其优点是使模板与组件中的逻辑更加清晰:
Template模板
<ul>
<li v-for="(value, index) in filtered" :key="index">
{{ value }}
</li>
</ul>
Options API选项 API
data: () => ({
array1: [1,2,3,4,5]
}),
computed: {
filtered() {
return this.array1.map(item => `$${item}`);
}
}
Composition API组成 API
setup() {
const array1 = ref([1,2,3,4,5]);
const filtered = computed(() => array1.value.map(item => `$${item}`));
return {
filtered
}
}
If you want to set it as a global filter and make it available to all your app components, do the following:如果要将其设置为全局过滤器并使其可用于所有应用程序组件,请执行以下操作:
const app = createApp(App)
app.config.globalProperties.$filters = {
valueFilter(value) {
return '$' + value
}
}
<template>
<ul>
<li v-for="(index,value) in array1" :key="index">
{{ $filters.valueFilter(value) }}
</li>
</ul>
</template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.