Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use filters filter in Vue

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要为大家展示了"Vue中如何使用filters过滤器",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Vue中如何使用filters过滤器"这篇文章吧。

Vue.js 允许我们自定义过滤器,可被用于一些常见的文本格式化。过滤器可以用在两个地方:双花括号({undefined{ }})插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持)。过滤器应该被添加在 JavaScript 表达式的尾部,由"管道"符号指示。

{{ message | capitalize }} 一、全局过滤器

定义一个全局过滤器很简单,只需要导出一个方法即可。

使用的时候很简单,只需要在入口文件全局引入此过滤器即可,使用 Vue.filter(key, value) 引入。

比如,Java后端返回的时间戳精确到秒,而JS中的时间戳是用毫秒表示,则可以定义一个转换时间戳的全局过滤器:

//main.jsimport Vue from 'vue'Vue.filter('millisecond', (value) => { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1)})

在需要使用的组件使用:

{{1516101106 | millisecond}}

全局过滤器之单一挂载

/** * dateTmp:要过滤的值 * fmtTmp:传入的参数,可接收多个参数 */ {{ 1639998858000 | dateFormat("yyyy/MM/dd HH:mm:ss") }} Vue.filter('dateFormat', function (dateTmp, fmtTmp) { let fmt = fmtTmp let date = dateTmp if (!fmt) { fmt = 'yyyy.MM.dd' } if (!(date instanceof Date)) { date = new Date(date) } let o = { 'M+': date.getMonth() + 1, // 月份 'd+': date.getDate(), // 日 'h+': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 小时 'H+': date.getHours(), // 小时 'm+': date.getMinutes(), // 分 's+': date.getSeconds(), // 秒 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度 'S': date.getMilliseconds() // 毫秒 } let week = { '0': '日', '1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六' } if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[date.getDay() + '']) } for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) } } return fmt});

全局过滤器之批量挂载

//定义方法//filters.jsexport function slice (temp,num) { return temp.slice(0,num);}//挂载//main.jsimport * as filters from '@/assets/js/filters'; Object.keys(filters).forEach(key => { Vue.filter(key, filters[key]);});//调用{{ '123456' | slice(4) }} 二、组件过滤器

组件过滤器更简单,只需在对应组件中定义 filters 即可,不过只针对本组件有效。

比如定义一个首字母大写的过滤器:

//定义方法export default { filters: { capitalize: function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) } }}

例子例子

{{ msg | setSize }} export default { data() { return { msg: "我要被过滤", }; }, filters: { setSize(value) { if (value.length > 4) { return value.splice(0, 4) + "..."; } else { return value; } }, },};以上是"Vue中如何使用filters过滤器"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report