过滤器(filter)的基本使用 + 时间戳转化为相对日期过滤器代码封装

简介: 过滤器(filter)的基本使用 + 时间戳转化为相对日期过滤器代码封装

过滤器(filter)的基本使用

概述

  1. 过滤器实质不改变原始数据,只是对数据进行加工处理后,return回过滤后的数据再进行调用处理
  2. 过滤器:本质是就是一个函数,用来做格式转换。例如:把一个标准时间转成相对时间。

格式

  • 局部定义
    filters:{
    过滤器名字(要被处理的值){
    // …
    return 处理后的结果
    }
    }
  • 局部使用
    {{ 原数据 | 过滤器名字 }}
    // 相当于 {{ 过滤器名(原数据) }}
  • 全局定义
    Vue.filter(‘过滤器名’, (oldVal) => {
    // …
    return 处理后的结果
    })
  • 全局使用
    {{ 原数据 | 过滤器名 }}
    // 相当于 {{ 过滤器名(原数据) }}

Vue组件中过滤器分为两种

  • 全局过滤器
    {{ filterElement | filterName }}
  • 局部过滤器
    // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
    el: ‘#app’,
    data: {
    msg: ‘曾经,我也是一个单纯的少年,单纯的我,傻傻的问,谁是世界上最单纯的男人’
    },
    methods: {},
//定义私用局部过滤器。只能在当前 vue 对象中使用
  filters: {
       dataFormat(msg) {
              return msg+'xxxxx';
       }
   }
  • });

注意

  • 当有局部和全局两个名称相同的过滤器时候,会以就近原则进行调用,即:局部过滤器优先于全局过滤器被调用!
  • 一个表达式可以使用多个过滤器。过滤器之间需要用管道符“|”隔开。其执行顺序从左往右
  • 全局过滤器注册时是filter,没有s。而局部过滤器注册时是filters,是有s的

移动端项目—封装时间戳转化相对日期过滤器

功能展示图

逐级优化:局部过滤器 —> 封装全局过滤器 —> 再封装成插件,使用Vue.use

  • 局部过滤器
    relativeTime (val) {
    const t = new Date(val)
    const diff = Date.now() - t.getTime()
const year = Math.floor(diff / (1000 * 3600 * 24 * 365))
    if (year) {
      return `${year}年前`
    }
    const month = Math.floor(diff / (1000 * 3600 * 24 * 30))
    if (month) {
      return `${month}月前`
    }
    const day = Math.floor(diff / (1000 * 3600 * 24))
    if (day) {
      return `${day}天前`
    }
    const hour = Math.floor(diff / (1000 * 3600))
    if (hour) {
      return `${hour}小时前`
    }
    const minute = Math.floor(diff / (1000 * 60))
    if (minute) {
      return `${minute}分钟前`
    } else {
      return '刚才'
    }
  }

使用:

<!-- 文字区域 -->
<div class="meta">
  <span>{{article.aut_name}}</span>
  <span>{{article.comm_count}}评论</span>
  // 被过滤数据 | 过滤器名字
+   <span>{{article.pubdate | relativeTime}}</span>
</div>
  • 全局过滤器(在main.js中,添加全局过滤器)
    // 全局过滤器
    Vue.filter(‘relativeTime’, (oldTime) => {
    const t = new Date(oldTime)
    // Date.now():现在的时间戳(毫秒)
    // t.getTime():旧时间的时间戳(毫秒)
const diff = Date.now() - t.getTime() // 相隔多少毫秒
// Math.floor 向下取整: 1.7年 ---> 1年前
const year = Math.floor(diff / (1000 * 3600 * 24 * 365))
if (year) {
  return `${year}年前`
}
const month = Math.floor(diff / (1000 * 3600 * 24 * 30))
if (month) {
  return `${month}月前`
}
const day = Math.floor(diff / (1000 * 3600 * 24))
if (day) {
  return `${day}天前`
}
const hour = Math.floor(diff / (1000 * 3600))
if (hour) {
  return `${hour}小时前`
}
const minute = Math.floor(diff / (1000 * 60))
if (minute) {
  return `${minute}分钟前`
} else {
  return '刚刚'
}
  • })
  • 把过滤器单独封装成一个插件,以便多项目复用
  1. 在utils创建filters.js
  • // 日期过滤器单独封装的模块
    export const relativeTime = (oldtime) => {
    // 将旧的时间转为时间戳
    const t = new Date(oldtime)
    // 现在时间戳 - 旧时间戳
    const diff = Date.now() - t.getTime()
// Math.floor 向下取整
const year = Math.floor(diff / (1000 * 3600 * 24 * 365))
if (year) {
  return `${year}年前`
}
const month = Math.floor(diff / (1000 * 3600 * 24 * 30))
if (month) {
  return `${month}月前`
}
const day = Math.floor(diff / (1000 * 3600 * 24))
if (day) {
  return `${day}天前`
}
const hour = Math.floor(diff / (1000 * 3600))
if (hour) {
  return `${hour}小时前`
}
const minute = Math.floor(diff / (1000 * 60))
if (minute) {
  return `${minute}分种前`
} else {
  return '刚才'
}
  • }
    export default {
    install (Vue) {
    // 全局过滤器(日期过滤器)过滤器名字 上面封装的函数名
    Vue.filter(‘relativeTime’, relativeTime)
    }
    }
  1. 在main.js中导入该插件,并使用(Vue.use())
    // 导入日期过滤器
    import filter from ‘./utils/filters’
    // filter中有叫relativeTime的过滤器
    Vue.use(filter)
  2. 使用:{{article.aut_name}} {{article.comm_count}}评论
// 被过滤数据 | 过滤器名字
  • {{article.pubdate | relativeTime}}

图解filter插件封装

用Vue.use()以插件的格式来导入过滤器的功能

参考:Vue.use

操作:

  1. 在filters.js中默认导出一个对象,其中有一个install方法 ------- 默认导出的就是一个vue插件。
  2. 在main.js中导入这个插件,并使用(Vue.use())

目录
相关文章
element-plus:el-date-picker日期只选择年月不要日
element-plus:el-date-picker日期只选择年月不要日
1390 0
|
JavaScript 前端开发 API
【前端用法】jQuery在线引用地址(全)
【前端用法】jQuery在线引用地址(全)
2599 0
|
11月前
|
存储 人工智能 安全
阿里云服务器通用型g7、g8a、g8y、g8i实例区别及选择指南
目前在阿里云的活动中,属于通用型实例规格的云服务器有通用型g7、通用型g8a、通用型g8y和通用型g8i这几个实例规格,相比于活动内的经济型e和通用算力型u1等实例规格来说,这些实例规格等性能更强,虽然这几个实例规格的云服务器通常处理器与内存的配比为都是1:4,但是他们在处理器、存储、网络、安全等方面等性能并不是一样的,所以他们的适用场景也有着不同。本文为大家介绍通用型g7、g8a、g8y、g8i实例的性能、适用场景的区别以及选择参考。
|
12月前
|
SQL 数据可视化 BI
SQL语句及查询结果解析:技巧与方法
在数据库管理和数据分析中,SQL语句扮演着至关重要的角色
1591 0
|
SQL 安全 Java
安全测试之推荐工具
【2月更文挑战第2天】安全测试之推荐工具
1004 2
|
12月前
|
SQL 监控 关系型数据库
如何查看MySQL使用的内存
综合运用上述方法,您可以全方位地监控和管理MySQL的内存使用。从简单查看配置到深入分析实时内存占用,每种方法都有其适用场景和优势。定期检查和调整MySQL的内存配置,对于维持数据库性能和稳定性至关重要。
1473 0
|
Prometheus 监控 Cloud Native
Grafana 入门指南:快速上手监控仪表盘
【8月更文第29天】Grafana 是一款开源的数据可视化和监控工具,它允许用户轻松地创建美观的仪表盘和图表,以便更好地理解和监控数据。无论您是需要监控系统性能指标、应用程序日志还是业务关键指标,Grafana 都能提供灵活而强大的解决方案。本指南将带领您快速上手 Grafana,包括安装、配置以及创建第一个监控面板。
2732 2
|
SQL 关系型数据库 MySQL
MySQL删除表数据、清空表命令(truncate、drop、delete 区别)
MySQL删除表数据、清空表命令(truncate、drop、delete区别) 使用原则总结如下: 当你不需要该表时(删除数据和结构),用drop; 当你仍要保留该表、仅删除所有数据表内容时,用truncate; 当你要删除部分记录、且希望能回滚的话,用delete;
|
JavaScript
|
XML 前端开发 测试技术
【实训项目】传道学习助手APP设计
【实训项目】传道学习助手APP设计
190 0