• 前端界面显示当前时间的Vue代码


    <!DOCTYPE html> 
    <html> 
    <head> <meta charset="utf-8">
        <title>Vue 示例</title>
    </head> 
    <body>
        <div id="app" >
          {{ date | formatDate }}
        </div>
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
    <script>
        var padDate = function(value){
            return value < 10 ? '0' + value :value;
        }
        var app = new Vue({
            el: '#app',
            data: {
                date: new Date()
            },
            filters: {
                formatDate: function (value) {
                    var date = new Date(value);
                    var year = date.getFullYear();
                    var month = padDate(date.getMonth() + 1);
                    var day = padDate(date.getDay());
                    var hours = padDate(date.getHours());
                    var minutes = padDate(date.getMinutes());
                    var seconds = padDate(date.getSeconds());
                    // 整理并返回
                    return year + '-' + month + '-' + day + '-' + hours + '?' + minutes + '?' + seconds;
                }
            },
            mounted: function () {
                var _this = this; // 指向this 变量作用域一致;
                this.timer = setInterval(function() {
                    _this.date = new Date(); // 修改date
                },1000);
            },
            beforeDestroy: function() {
                if (this.timer) {
                    clearInterval(this.timer);
                }
            }
            // created: function () {
            //     console.log(this.a);
            // },
            // mounted: function () {
            //     var _this = this; //声明一个变量指向 Vue 实例 this,保证作用域一致
            //     this.timer = setInterval (function () {
            //         this.date = new Date();
            //         console.log(this.date);// 修改date
            //     },1000);
                
            // },
            // beforeDestory: function () {
            //     if (this.timer) {
            //         clearInterval(this.timer);  //在 Vue 实例销毁前,清除我们的定时器
    
            //     } 
            // }
        })
    </script>
    </body>
    </html>
    

      

    新鲜刺激的东西永远都有,玩之前掂量掂量自己几斤几两
  • 相关阅读:
    线性表3 数据结构和算法08
    线性表3 数据结构和算法08
    线性表的链式存储结构
    OD使用教程9 调试篇09|解密系列
    线性表
    线性表
    线性表
    OD使用教程9 调试篇09|解密系列
    验证中英文数字和下划线中划线
    formEl.submit is not a function的原因
  • 原文地址:https://www.cnblogs.com/banxianer/p/13689917.html
Copyright © 2020-2023  润新知