美文网首页
过滤器和计算函数

过滤器和计算函数

作者: 郭佳伟666 | 来源:发表于2018-09-17 17:12 被阅读0次
    让要显示在页面上的内容进行重新筛选,分为两种:
    1.全局过滤器     2.局部过滤器
    1.全局过滤器
    例:
    html部分
            <div id="itany">
                <p>{{new Date()|time}}</p>
            </div>
    
    js部分
           <script>
                new Vue({
                    el: "#itany",
                    filters: {
                        time: function(data) {
                            return data.getFullYear() + '年' + (data.getMonth() + 1) + '月' + data.getDate() + '日,星期' + data.getDay() + ',' + data.getHours() + '点' + data.getMinutes() + '分' + data.getSeconds() + '秒';
    
                        }
                    }
    
                })
            </script>
    
    2.局部过滤器
    例:
    html部分
            <div id="itany">
                <p>{{12|zore}}</p>
            </div>
    

    js部分

            <script>
                Vue.filter("zore",function(data){
                    if(data<10){
                        return "0"+data;
                    }else{
                        return data;
                    }
                })
                new Vue({
                    el:"#itany"
                })
            </script>
    
    3.计算函数
    例1:
    html部分
            <div id="itany">
                <h1>{{count}}</h1>
            </div>
    
    js部分
          <script>
                new Vue({
                    el: "#itany",
                    data: {
                        msg: "hello vue"
                    },
                    computed: {
                        count: function() {
                            return this.msg.split(" ").reverse().join("===")
                        }
                    }
                })
            </script>
    
    例2:
    html部分
            <div id="itany">
                <h1>{{count}}</h1>
            </div>
    
    js部分
           <script>
                new Vue({
                    el: "#itany",
                    data: {
                        msg: "hello vue"
                    },
                    computed: {
                        count: function() {
                            return this.msg.split(" ").reverse().join("===")
                        }
                    }
                })
            </script>
    

    相关文章

      网友评论

          本文标题:过滤器和计算函数

          本文链接:https://www.haomeiwen.com/subject/rlfrnftx.html