美文网首页工作生活
2019-07-03vue之私有过滤器

2019-07-03vue之私有过滤器

作者: 果冻_4c9b | 来源:发表于2019-07-03 15:26 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <div id="app">
        <p>{{msg1 | dateFormat}}</p>
    </div>

    <div id="app1">
        <p>{{msg2 | dateFormat2}}</p>
    </div>


    <script src="./js/vue.js"></script>
    <script>
        function dategetTwo(date) {
            return date < 10 ? '0' + date : date;
        }
        // 整理日期
        // Vue.filter('dateformat',function(data,arg0){
        //     return data.getFullYear()+"-"+dategetTwo((data.getMonth()+1))+"-"+dategetTwo(data.getDate());

        // })
        Vue.filter('dateformat', function (data, arg0) {
            var y = data.getFullYear().toString().padStart(2, '0');
            var m = (data.getMonth() + 1).toString().padStart(2, '0');
            var d = data.getDate().toString().padStart(2, '0');

            // return y +'-'+m+'-'+d;
            // return `${dategetTwo(y)}-${dategetTwo(m)}-${dategetTwo(d)}`
            return `${y}-${m}-${d}`
        })
        var vm = new Vue({
            el: "#app",
            data: {
                msg1: new Date(),
            },
        })

        var vm2 = new Vue({
            el: "#app1",
            data: {
                msg2: new Date(),
            },
            methods: {},
            // 私有过滤器,只对某个vue实例起作用
            filters: {
                dateFormat2: function (data, arg0) {
                    var y = data.getFullYear().toString().padStart(2, "0");
                    var m = (data.getMonth() + 1).toString().padStart(2, "0");
                    var d = data.getDate().toString().padStart(2, "0");

                    return `你好${y}-${m}-${d}`;
                }
            }
        })
    </script>

</body>

</html>

相关文章

网友评论

    本文标题:2019-07-03vue之私有过滤器

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