Vue2.0系列之过滤器的使用

  

vue2.0已经废弃了过滤器,需要自定义过滤器,用于一些常见的文本格式化。
  

  

感觉超级好用! !
  

  

过滤器可以用在两个地方:双花括号插值和v-bind表达式。

  

过滤器应该被添加在JavaScript表达式的尾部,由管道符指示。

  

  

注意事项:
  1,全局方法Vue.filter()注册一个自定义过滤器,必须放在vue实例化前面
  2、过滤器函数始终以表达式的值作为第一个参数,带引号的参数视为字符串,而不带引号的参数按表达式计算
  3,可以设置两个过滤器参数,前提这两个过滤器处理的不冲突
  4、用户从输入输入的数据在会传到模型之前也可以先处理

  

案例         & lt; !DOCTYPE html>   & lt; html lang=癳n”比;   & lt; head>   & lt;元charset=皍tf - 8”比;   & lt; title>过滤器& lt;/title>   & lt;/head>   & lt; body>      & lt; div id=坝τ谩北?   & lt; !——首字符串大写——比;   & lt; div>首字母大写过滤器:{{str | upcase}} & lt;/div>   & lt; !——给过滤器传入参数——比;   & lt; p>求和过滤器:{{消息|总和(10、20)}}& lt;/p>   & lt;/div>      & lt;脚本type=" text/javascript " src=' https://i0.jrjimg.cn/zqt-red-1000/focus/focus2017YMZ/teamFrighting/js/vue.min.js '祝辞& lt;/script>   & lt;脚本type=" text/javascript祝辞//全局方法Vue.filter()注册一个自定义过滤器,必须放在Vue实例化前面//注册一个首字母大写的过滤器   Vue。过滤器(“upcase”、功能(价值){   如果(价值)返回"   值=https://www.yisu.com/zixun/value.toString ()   返回value.charAt (0) .toUpperCase () + value.slice (1)   });//全局注册一个求和过滤器   Vue。过滤器(“求和”,函数(价值,a, b) {   返回值+ a + b;   });      Vue ({var=new演示   埃尔:“#应用”,   数据:{   str:“你好”,   消息:12   }   });   & lt;/script>   & lt;/body>   & lt;/html>      

案例效果:

  

 Vue2.0系列之过滤器的使用“> </p>
  <p> </p>
  <p>过滤器也可以注册在实例内部,仅在使用它的实例里面注册。</p>
  <p>根据以上案例改编:</p>
  
  <pre类=   & lt; !DOCTYPE html>   & lt; html lang=癳n”比;   & lt; head>   & lt;元charset=皍tf - 8”比;   & lt; title> & lt;/title>   & lt;/head>   & lt; body>      & lt; div id=坝τ谩北?   & lt; !——首字符串大写——比;   & lt; div>首字母大写过滤器:{{str | upcase}} & lt;/div>   & lt; !——给过滤器传入参数——比;   & lt; p>求和过滤器:{{消息|总和(10、20)}}& lt;/p>   & lt;/div>      & lt;脚本type=" text/javascript " src=' https://i0.jrjimg.cn/zqt-red-1000/focus/focus2017YMZ/teamFrighting/js/vue.min.js '祝辞& lt;/script>   & lt;脚本type=" text/javascript祝辞   Vue ({var=new演示   埃尔:“#应用”,   数据:{   str:“你好”,   消息:12   },   过滤器:{   upcase:功能(价值){   如果(价值)返回"   值=https://www.yisu.com/zixun/value.toString ()   返回value.charAt (0) .toUpperCase () + value.slice (1)   },   总和:函数(价值,a, b) {   返回值+ a + b;   }   }   });>         之前      

效果:   

 Vue2.0系列之过滤器的使用“> </p>
  <p> </p>
  <p>根据时间戳转化成时间格式:mm-dd hh: tt </p>
  
  <pre类=   & lt; !DOCTYPE html>   & lt; html lang=癳n”比;   & lt; head>   & lt;元charset=皍tf - 8”比;   & lt; title> & lt;/title>   & lt;/head>   & lt; body>      & lt; div id=坝τ谩北?   & lt; !——将时间戳转化为时间——比;   & lt; h2>根据时间戳转化为时间:{{str | formateTime}} & lt;/h2>   & lt;/div>      & lt;脚本type=" text/javascript " src=' https://i0.jrjimg.cn/zqt-red-1000/focus/focus2017YMZ/teamFrighting/js/vue.min.js '祝辞& lt;/script>   & lt;脚本type=" text/javascript祝辞   Vue ({var=new演示   埃尔:“#应用”,   数据:{   str: 1517568434324,   },   过滤器:{   formateTime:函数(nS) {   返回((新日期(nS) .getMonth () + 1) & lt; 10 & # 63;“0”+(新日期(nS) .getMonth() + 1):(新日期(nS) .getMonth() + 1)) +“-”+(新的日期(nS) .getDate () & lt; 10 & # 63; ' 0 ' +新日期(nS) .getDate():新的日期(nS) .getDate()) + " +(新的日期(nS) .getHours () & lt; 10 & # 63; ' 0 ' +新日期(nS) .getHours():新的日期(nS) .getHours()) +“:”+(新的日期(nS) .getMinutes () & lt; 10 & # 63; ' 0 ' +新日期(nS) .getMinutes():新的日期(nS) .getMinutes ())   }   }   });      & lt;/script>   & lt;/body>   & lt;/html>   

Vue2.0系列之过滤器的使用