zoukankan      html  css  js  c++  java
  • Vue filters过滤器的使用方法

    来源:http://www.jb51.net/article/118695.htm

    先来看看一段代码理解下 

    html

    1
    2
    3
    <div id="app">
      {{message | filters2| filters3(true,priceCount)}}
    </div>

    js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    var app = new Vue({
      el: "#app",
      data: {
        message: 199,
        priceCount:.8
      },
      filters:{
        filters2:function (arg) {
          console.log("arg:"+arg)
          if(arg>100){
            return arg-8;
          }else {
            return arg;
          }
        },
        filters3:function (arg_1,arg_2,arg_3) {
          var result;
          console.log("arg_1:"+arg_1)
          console.log("arg_2:"+arg_2)
          console.log("arg_3:"+arg_3)
          if(arg_2){
            result = arg_1*arg_3;
            console.log("result"+result);
            return result;
          }else{
            result =arg_1;
            console.log("result"+result);
            return result
          }
     
        }
      }
    });

    控制台日志

    helloVue.js:17 arg:199
    helloVue.js:26 arg_1:191
    helloVue.js:27 arg_2:true
    helloVue.js:28 arg_3:0.8
    helloVue.js:35 result152.8

    先来看看两个过滤器的入参 

    第一个过滤器filters2的入参是199,是Vue实例中绑定的message 
    第二个过滤器filters3的入参是191、(第一个过滤器返回的值)false(第二个过滤器的第一个入参)、0.8(第二个过滤器的第二个入参)

    1、Vue实例中的message是199 
    2、第一个过滤器,大于100的数减8(理解为满100减8),199-8=191传给第二个过滤器作为第一个参数 
    3、第二个过滤器,有两个入参,第一个是boolean值(理解为是否打折),第二个是0.8(折扣)。 

    当第一个入参为true的时候(表示需要打折),191*0.8=152.8(0.8为折扣)

    总结

    通过过滤器filters我们可以根据业务场景对数据进行处理。 

    如上一个例子,可以理解成如下业务场景。 

    1、当商品价格大于100时,可以减8块钱 
    2、当店铺做活动的时候可以进行打折促销(0.8) 
    3、最后展示的数值就是用户需要支付的金额

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

     
  • 相关阅读:
    zend studio常见问题解答
    瀑布流插件(jquery.masonry.js)
    仿jQuery中undelegate()方法功能的函数
    Linux 常用命令
    linux debugfs 找回rm 的文件
    jq 添加和移除样式
    CentOS 搭建 nginx python django web server
    Linux vim 配置文件
    CentOS 安装python 3.3.2
    login.defs和shadow文件区别
  • 原文地址:https://www.cnblogs.com/liangru/p/8916626.html
Copyright © 2011-2022 走看看