Vue.js -- 过滤器使用总结
作者:Jone_chen 时间:2024-05-09 10:41:28
过滤器是一个通过输入数据,能够及时对数据进行处理并返回一个数据结果的简单函数。Vue有很多很便利的过滤器,过滤器通常会使用管道标志 “ | ”, 比如:
{{ msg | capitalize }}
// 'abc' => 'ABC'
uppercase过滤器 : 将输入的字符串转换成大写字母的过滤器。
VueJs允许你链式调用过滤器,简单的来说,就是一个过滤器的输出成为下一个过滤器的输入,然后再次过滤。接下来,我们可以想象一个比较简单的例子,使用了Vue的 filterBy + orderBy 过滤器来过滤所有商品products。过滤出来的产品是属于水果类商品。
filterBy过滤器 : 过滤器的值必须是一个数组,filterBy + 过滤条件。过滤条件是:'string || function'+ in 'optionKeyName'
orderBy过滤器 : 过滤器的值必须是一个数组,orderBy + 过滤条件。过滤条件是:'string || array ||function' + 'order ≥ 0 为升序 || order < 0 为降序'
接下来,我们可以看下下面这个例子:我们有一个商品数组products,我们希望遍历这个数组,并把他们打印成一张清单,这个用v-for很容易实现。
<ul class="product">
<li v-for="product in products|filterBy '水果' in 'category' |orderBy 'price' 1">
{{product.name}}-{{product.price | currency}}
</li>
</ul>
上面的例子,就是用filterBy 过滤在 'category'中含有'水果' 关键字的列表,返回的列表就是只含有 '水果' 关键字的列表,而orderBy过滤器是根据价格做了一个升序,如果想要降序,只需要加一个小于0的参数;
自定义过滤器
虽然VueJs给我们提供了很多强有力的过滤器,但有时候还是不够。值得庆幸的,Vue给我们提供了一个干净简洁的方式来定义我们自己的过滤器,之后我们就可以利用管道 “ | ” 来完成过滤。
定义一个全局的自定义过滤器,需要使用Vue.filter()构造器。这个构造器需要两个参数。
Vue.filter() Constructor Parameters:
1.filterId: 过滤器ID,用来做为你的过滤器的唯一标识;
2.filter function: 过滤器函数,用一个function来接收一个参数,之后再将接收到的参数格式化为想要的数据结果。
上面的例子中,我们要实现商品价格打5折该怎么做呢?其实就是实现的一个自定义的过滤器,表示将商品的价格打了5折;而要实现它,需要完成的是:
a、使用Vue.filter()构造器创建一个过滤器叫做discount
b、输入商品的原价,能够返回其打五折之后的折扣价
代码见下面:
Vue.filter('discount', function(value) {
return value * .5;
});
var product = new Vue({
el: '.product',
data: {
products: [
{name: '苹果',price: 25,category: "水果"},
{name: '香蕉',price: 15,category: "水果"},
{name: '雪梨',price: 65,category: "水果"},
{name: '宝马',price: 2500,category: "汽车"},
{name: '奔驰',price: 10025,category: "汽车"},
{name: '柑橘',price: 15,category: "水果"},
{name: '奥迪',price: 25,category: "汽车"}
]
},
})
现在就可以像使用Vue自带的过滤器一样使用自定义过滤器了
<ul class="product">
<li v-for="product in products|filterBy '水果' in 'category' |orderBy 'price' 0">
{{product.name}}-{{product.price|discount | currency}}
</li>
</ul>
上面代码实现的商品打5折,而如果要实现价格打任意折扣呢?应该在discount过滤器里增加一个折扣数值参数,改造一下我们的过滤器。
Vue.filter('discount', function(value, discount) {
return value * (discount / 100);
});
然后重新调用我们的过滤器
<ul class="product">
<li v-for="product in products|filterBy '水果' in 'category' |orderBy 'price' 0">
{{product.name}}-{{product.price|discount 25 | currency}}
</li>
</ul>
我们也可以在我们Vue实例里构造我们的过滤器,这样构造的好处是,这样就不会影响到其他不需要用到这个过滤器的Vue实例。
/*定义在全局
Vue.filter('discount', function(value,discount) {
return value * ( discount / 100 ) ;
});
*/
var product = new Vue({
el: '.product',
data: {
products: [
{name: '苹果',price: 25,category: "水果"},
{name: '香蕉',price: 15,category: "水果"},
{name: '雪梨',price: 65,category: "水果"},
{name: '宝马',price: 2500,category: "汽车"},
{name: '奔驰',price: 10025,category: "汽车"},
{name: '柑橘',price: 15,category: "水果"},
{name: '奥迪',price: 25,category: "汽车"}
]
},
//自定义在实例
filters: {
discount: function(value, discount) {
return value * (discount / 100);
}
}
})
定义在全局就能在所有的实例中调用过滤器,如果定义在了实例里就在实例里调用过滤器。
来源:http://www.cnblogs.com/jone-chen/p/5846500.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现制作透明背景的电子印章
![](https://img.aspxhome.com/file/2023/1/134961_0s.jpg)
培养色感的一些经验分享
![](https://img.aspxhome.com/file/UploadPic/20071/200713110530379s.gif)
对python使用http、https代理的实例讲解
VSCode远程连接服务器报错:Could not establish connection to
![](https://img.aspxhome.com/file/2023/9/114709_0s.png)
使用Python进行二进制文件读写的简单方法(推荐)
MySQL修改默认字符集
基于DataFrame改变列类型的方法
django自定义非主键自增字段类型详解(auto increment field)
python实现在内存中读写str和二进制数据代码
深度学习Tensorflow2.8实现GRU文本生成任务详解
使用Python神器对付12306变态验证码
![](https://img.aspxhome.com/file/2023/5/118345_0s.gif)
使用urllib库的urlretrieve()方法下载网络文件到本地的方法
![](https://img.aspxhome.com/file/2023/3/107803_0s.jpg)
python 实现读取一个excel多个sheet表并合并的方法
解析isset与is_null的区别
老生常谈js中0到底是 true 还是 false
vue实现手机号码的校验实例代码(防抖函数的应用场景)
![](https://img.aspxhome.com/file/2023/2/123182_0s.png)
Python实现的KMeans聚类算法实例分析
![](https://img.aspxhome.com/file/2023/4/88974_0s.png)
解决python执行不输出系统命令弹框的问题
python实现从文件中读取数据并绘制成 x y 轴图形的方法
![](https://img.aspxhome.com/file/2023/5/94005_0s.jpg)
js游戏 俄罗斯方块 源代码
![](https://img.aspxhome.com/file/UploadPic/20081/24/200812414337986s.jpg)