当前位置: 首页 > news >正文

Vue-过滤器

过滤器

时间戳格式化

实现方式

  • 计算属性
  • 方法
  • 过滤器

基础依赖

  • day.min.js 下载链接
  • 放到 相对路径 js 目录下

Computed

  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>过滤器</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><script type="text/javascript" src="../js/dayjs.min.js"></script><style>.base {padding: 5px;height: 100px;}</style></head><body><div id="root"><h1>过滤器-格式化时间</h1><div><h2>当前时间戳:{{curTime}}</h2><!-- 计算属性 --><h3>计算属性:{{fmtTime}}</h3></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",// curTime: new Date().getTime(),curTime: 1748675500506,},computed: {fmtTime(){console.log(" computed format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},methods: {},});</script>
</html>
  • 效果

在这里插入图片描述

Methods

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>过滤器</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><script type="text/javascript" src="../js/dayjs.min.js"></script><style>.base {padding: 5px;height: 100px;}</style></head><body><div id="root"><h1>过滤器-格式化时间</h1><div><h2>当前时间戳:{{curTime}}</h2><!-- 计算属性 --><h3>计算属性:{{fmtTime}}</h3><!-- Methods方法 --><h3>Methods方法:{{formatTime()}}</h3></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",// curTime: new Date().getTime(),curTime: 1748675500506,},computed: {fmtTime(){console.log(" computed format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},methods: {formatTime(){console.log(" methods format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},});</script>
</html>
  • 效果

在这里插入图片描述

Filters

无参过滤器
  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>过滤器</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><script type="text/javascript" src="../js/dayjs.min.js"></script><style>.base {padding: 5px;height: 100px;}</style></head><body><div id="root"><h1>过滤器-格式化时间</h1><div><h2>当前时间戳:{{curTime}}</h2><!-- 计算属性 --><h3>计算属性:{{fmtTime}}</h3><!-- Methods方法 --><h3>Methods方法:{{formatTime()}}</h3><!-- 过滤器 --><h3>无参过滤器:{{curTime | timeFormater}}</h3></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",// curTime: new Date().getTime(),curTime: 1748675500506,},computed: {fmtTime(){console.log(" computed format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},methods: {formatTime(){console.log(" methods format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},filters: {timeFormater(value){console.log(" No args filters format time ...")return dayjs(value).format('YYYY-MM-DD HH:mm:ss')}}});</script>
</html>
  • 效果

在这里插入图片描述

有参过滤器
  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>过滤器</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><script type="text/javascript" src="../js/dayjs.min.js"></script><style>.base {padding: 5px;height: 100px;}</style></head><body><div id="root"><h1>过滤器-格式化时间</h1><div><h2>当前时间戳:{{curTime}}</h2><!-- 计算属性 --><h3>计算属性:{{fmtTime}}</h3><!-- Methods方法 --><h3>Methods方法:{{formatTime()}}</h3><!-- 过滤器 --><h3>无参过滤器:{{curTime | timeFormater}}</h3><h3>有参过滤器:{{curTime | timeFormater('YYYY年MM月DD日 HH时mm分ss秒')}}</h3></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",// curTime: new Date().getTime(),curTime: 1748675500506,},computed: {fmtTime(){console.log(" computed format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},methods: {formatTime(){console.log(" methods format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},filters: {timeFormater(value,formatStr='YYYY-MM-DD HH:mm:ss'){console.log(" filters format time ... :" + formatStr)return dayjs(value).format(formatStr)}}});</script>
</html>
  • 效果

在这里插入图片描述

链式过滤器
  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>过滤器</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><script type="text/javascript" src="../js/dayjs.min.js"></script><style>.base {padding: 5px;height: 100px;}</style></head><body><div id="root"><h1>过滤器-格式化时间</h1><div><h2>当前时间戳:{{curTime}}</h2><!-- 计算属性 --><h3>计算属性:{{fmtTime}}</h3><!-- Methods方法 --><h3>Methods方法:{{formatTime()}}</h3><!-- 过滤器 --><h3>无参过滤器:{{curTime | timeFormater}}</h3><h3>有参过滤器:{{curTime | timeFormater('YYYY年MM月DD日 HH时mm分ss秒')}}</h3><h3>链式过滤器:{{curTime | timeFormater('YYYY年MM月DD日 HH时mm分ss秒') | spliceStr}}</h3></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",// curTime: new Date().getTime(),curTime: 1748675500506,},computed: {fmtTime(){console.log(" computed format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},methods: {formatTime(){console.log(" methods format time ...")return dayjs(this.curTime).format('YYYY-MM-DD HH:mm:ss')}},filters: {timeFormater(value,formatStr='YYYY-MM-DD HH:mm:ss'){console.log(" filters format time ... :" + formatStr)return dayjs(value).format(formatStr)},spliceStr(value){console.log(" filters splice time (年月日)... ")return value.split(" ")[0]}}});</script>
</html>
  • 效果
    在这里插入图片描述
全局过滤器

格式: Vue.filter(‘filterName’,function(value){})
注意:全局过滤器声明必须在Vue实例化之前

  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>过滤器</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><script type="text/javascript" src="../js/dayjs.min.js"></script><style>.base {padding: 5px;height: 100px;}</style></head><body><div id="root"><h1>过滤器-格式化时间</h1><div><h2>当前时间戳:{{curTime}}</h2><!-- 计算属性 --><h3>计算属性:{{fmtTime}}</h3><!-- Methods方法 --><h3>Methods方法:{{formatTime()}}</h3><!-- 过滤器 --><h3>无参过滤器:{{curTime | timeFormater}}</h3><h3>有参过滤器:{{curTime | timeFormater('YYYY年MM月DD日 HH时mm分ss秒')}}</h3><h3>链式过滤器:{{curTime | timeFormater('YYYY年MM月DD日 HH时mm分ss秒') |spliceStr}}</h3><h3>全局过滤器:{{curTime | timeFormater | globalStr}}</h3></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示Vue.filter("globalStr", function (value) {console.log(" global filter splice time (时分秒)... ");return value.split(" ")[1];});const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",// curTime: new Date().getTime(),curTime: 1748675500506,},computed: {fmtTime() {console.log(" computed format time ...");return dayjs(this.curTime).format("YYYY-MM-DD HH:mm:ss");},},methods: {formatTime() {console.log(" methods format time ...");return dayjs(this.curTime).format("YYYY-MM-DD HH:mm:ss");},},filters: {timeFormater(value, formatStr = "YYYY-MM-DD HH:mm:ss") {console.log(" filters format time ... :" + formatStr);return dayjs(value).format(formatStr);},spliceStr(value) {console.log(" filters splice time (年月日)... ");return value.split(" ")[0];},},});</script>
</html>
  • 效果

在这里插入图片描述

相关文章:

  • 【PhysUnits】15.6 引入P1后的左移运算(shl.rs)
  • java8集合操作全集
  • linux 1.0.7
  • 《深入解析SPI协议及其FPGA高效实现》-- 第一篇:SPI协议基础与工作机制
  • MySQL索引与性能优化入门:让查询提速的秘密武器【MySQL系列】
  • AI 的早期萌芽?用 Swift 演绎约翰·康威的「生命游戏」
  • [蓝桥杯]机器人塔
  • Day 41
  • HackMyVM-Art
  • 关于win10系统中环境变量path变成一行显示的问题
  • Target店铺应该如何入驻?
  • Python训练营打卡Day41(2025.5.31)
  • 软件技术如何赚钱
  • 流媒体基础分析:延迟分析与安全性保障
  • Java Spring 之监听器(Listener)详解与实战
  • SoftThinking:让模型学会模糊思考,同时提升准确性和推理速度!!
  • JVM 基础 - JVM 内存结构
  • homework 2025.03.31 chinese(class 3)
  • 【Netty系列】解决TCP粘包和拆包:LengthFieldBasedFrameDecoder
  • c++第三章练习题
  • 建设中心小学网站/百度收录在线提交
  • 怎样利用网站做引流/排名优化公司哪家效果好
  • 关于成立政府网站建设/网站分析
  • 网盘网站建设/网站打开
  • 做公司网站 java php/搜索引擎营销方法
  • 我想看b站直播有哪些软件/首页