当前位置:网站首页>14.过滤器的使用
14.过滤器的使用
2022-07-19 05:05:00 【风落不归处】
定义:对要显示的数据进行特定格式化后再显示(适用于一些简单的逻辑处理)
语法:
1.注册过滤器:Vue.filter(name,callback)或new Vue{filters:{}}
2.使用过滤器:{ { xxx | 过滤器名 }}或v-bind:属性="xxx | 过滤器名"
备注:过滤器也可以接收额外的参数、多个过滤器也可以串联
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>过滤器</title>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript" src="../js/dayjs.min.js"></script>
</head>
<body>
<!--
过滤器:
定义:对要显示的数据进行特定格式化后再显示(适用于一些简单逻辑的处理)。
语法:
1.注册过滤器:Vue.filter(name,callback) 或 new Vue{filters:{}}
2.使用过滤器:{
{ xxx | 过滤器名}} 或 v-bind:属性 = "xxx | 过滤器名"
备注:
1.过滤器也可以接收额外参数、多个过滤器也可以串联
2.并没有改变原本的数据, 是产生新的对应的数据
-->
<!-- 准备好一个容器-->
<div id="root">
<h2>显示格式化后的时间</h2>
<!-- 计算属性实现 -->
<h3>现在是:{
{fmtTime}}</h3>
<!-- methods实现 -->
<h3>现在是:{
{getFmtTime()}}</h3>
<!-- 过滤器实现 -->
<h3>现在是:{
{time | timeFormater}}</h3>
<!-- 过滤器实现(传参) -->
<h3>现在是:{
{time | timeFormater('YYYY_MM_DD') | mySlice}}</h3>
<h3 :x="msg | mySlice">尚硅谷</h3>
</div>
<div id="root2">
<h2>{
{msg | mySlice}}</h2>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false
//全局过滤器
Vue.filter('mySlice',function(value){
return value.slice(0,4)
})
new Vue({
el:'#root',
data:{
time:1621561377603, //时间戳
msg:'你好,尚硅谷'
},
computed: {
fmtTime(){
return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')
}
},
methods: {
getFmtTime(){
return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')
}
},
//局部过滤器
filters:{
timeFormater(value,str='YYYY年MM月DD日 HH:mm:ss'){
// console.log('@',value)
return dayjs(value).format(str)
}
}
})
new Vue({
el:'#root2',
data:{
msg:'hello,atguigu!'
}
})
</script>
</html>
边栏推荐
猜你喜欢
进程与线程以及进线程间通信
第六十二篇:win10上运行VS程序出现蓝屏崩溃
Pytorch implements the use of data enhancement classification evaluations
数据在内存中存储是怎样的?
Pytorch target detection data processing (II) extracting difficult samples, low AP samples
二阶及N阶最长公共子序列
Pointer operation exercises and string functions
动态内存管理
【学习笔记】Solid Works 作业记录
83-局部组件的复用[父传子]
随机推荐
节流阀中的 flag=false if(flag){return} timer=null if(timer){return} 写法一直不太明白怎么起作用的
ES6之Object.defineProperty 和 Proxy 区别
C語言實現通訊錄
Easier to use C language entry-level cheese (2) select statements + loop statements, functions, arrays, operators (super detailed)
通过调试发现程序死循环的原因
二阶及N阶最长公共子序列
Custom type: structure, bit segment, enumeration, union
数据在内存中的存储
第六十四篇:error LNK2019: 无法解析的外部符号 cvRound
轻松掌握|C语言字符串与内存的相关库函数|
【学习笔记】Unreal(虚幻)4引擎入门(四)
长篇详解C语言动态内存管理
Interesting minesweeping game, C language programming
指针数组和数组指针有什么区别?
轻松掌握|struct结构体|知识点
更易上手的C语言入门级芝士 (1) 数据类型、变量和常量、字符串+转义字符+注释(超详细)
C语言中的指针(学习心得)
函数指针
【学习笔记】Unreal(虚幻)4引擎入门(三)
steam文件夹移动后游戏需要重新安装怎么办