vue实现的封装全局filter并统一管理操作示例
本文实例讲述了vue实现的封装全局filter并统一管理操作。分享给大家供大家参考,具体如下:
在前后端分离的项目中,经常会有后台返回的数据需要进过处理才能显示到页面上的场景。
使用最多的场景就是日期和时间的处理,后台一般返回的都是时间戳,那么我们就要对时间戳进行处理。
下面就拿封装全局的处理日期和时间的 filter 来展示如何 vue 如何封装全局 filter 并统一处理。
在 src 目录下新建 filters 目录用来专门存放全局过滤器,如果项目的过滤器过多,那么就要按类型分类。
我司的项目需要前台处理的数据不是太多,那么就在 filters 目录下新建一个 index.js 来存放所有的过滤器就足够了。
index.js 代码如下:
/*
日期处理
time:源时间戳
type:要处理的格式 默认 xxxx年xx月xx日
/: xxxx/xx/xx
.: xxxx.xx.xx
-: xxxx-xx-xx
*/
export const normalDate = (time,type) => {
if (time) {
var date = new Date();
date.setTime(time);
var year = date.getFullYear();
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) * 1 : date.getMonth() + 1;
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
if(type == '-'){
return year + '-' + month + '-' + day;
}else if(type == '/'){
return year + '/' + month + '/' + day;
}else if(type == '.'){
return year + '.' + month + '.' + day;
}else{
return year + '年' + month + '月' + day + '日';
}
}
}
/*
时间处理
time:源时间戳
type:要处理的格式 默认 xxxx年xx月xx日 xx:xx:xx
/: xxxx/xx/xx xx:xx:xx
.: xxxx.xx.xx xx:xx:xx
-: xxxx-xx-xx xx:xx:xx
*/
export const normalTime = (time,type) => {
if (time) {
var date = new Date();
date.setTime(time);
var year = date.getFullYear();
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) * 1 : date.getMonth() + 1;
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
if(type == '-'){
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}else if(type == '/'){
return year + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}else if(type == '.'){
return year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}else{
return year + '年' + month + '月' + day + '日' + ' ' + hours + ':' + minutes + ':' + seconds;
}
}
}
然后在 main.js 中引入注册即可使用:
import * as filters from './filters' Object.keys(filters).forEach(key => Vue.filter(key, filters[key]));
在页面中使用:
<p>{{time | normalDate('/')}}</p> //这样时间戳就会转化为xxxx/xx/xx的格式
希望本文所述对大家vue.js程序设计有所帮助。
相关文章
Vue 3 + Element Plus 封装单列控制编辑的可编辑表格
在Web应用开发中实现表格数据编辑功能至关重要,本文将详细介绍如何使用Vue3和ElementPlus库来构建一个支持单列编辑的表格组件,本教程详细阐述了组件创建过程和数据绑定机制,帮助你快速掌握Vue3中表格编辑功能的实现,感兴趣的朋友一起看看吧2024-09-09
详解vuex中mapState,mapGetters,mapMutations,mapActions的作用
这篇文章主要介绍了vuex中mapState,mapGetters,mapMutations,mapActions的作用,需要的朋友可以参考下2018-04-04
在Vue3项目中集成CodeMirror创建SQL编辑器的方法详解
在这篇文章中,我们将学习如何在 Vue 3 项目中集成 CodeMirror,以创建一个支持 SQL 语法高亮和自动补全的代码编辑器,需要的朋友可以参考下2025-04-04
如何配置vue.config.js 处理static文件夹下的静态文件
这篇文章主要介绍了如何配置vue.config.js 处理static文件夹下的静态文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-06-06
在移动端使用vue-router和keep-alive的方法示例
这篇文章主要介绍了在移动端使用vue-router和keep-alive的方法示例,vue-router与keep-alive提供的路由体验与移动端是有一定差别的,感兴趣的小伙伴们可以参考一下2018-12-12


最新评论