Vue每秒时间更新一次,傻瓜式实现时间灵活调动。

首先我们在 methods 中进行书写时间的方法,在methods定义年月日时分秒等进行一个初步构造。

getCurrentTime() {
        //获取当前时间并打印
        var _this = this;
    let yy = new Date().getFullYear();
    let mm = new Date().getMonth()+1;
    let dd = new Date().getDate();
    let hh = new Date().getHours();
    let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
    let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
    _this.gettime = yy+'-'+mm+'-'+dd+' '+hh+':'+mf+':'+ss;
    this.Samplingtime=_this.gettime;
    console.log(_this.gettime)  
    }

 然后在  Vue的周期函数中的mounted()里面进行设置每秒调用一次。

 mounted() {
     this.getCurrentTime();
    clearInterval(myTimeDisplay );
    let myTimeDisplay = setInterval(() => {
      this.getCurrentTime(); //每秒更新一次时间
    }, 1000);
  },

最终代码:

<template>
<div>{{Samplingtime}}</div>
</template>
export default {
  data() {
    return {
      //取样时间
      Samplingtime:'',
    };
  },
 mounted() {
     this.getCurrentTime();
    clearInterval(myTimeDisplay );
    let myTimeDisplay = setInterval(() => {
      this.getCurrentTime(); //每秒更新一次时间
    }, 1000);
  },
  

methods: {
    //时间 
    getCurrentTime() {
        //获取当前时间并打印
        var _this = this;
    let yy = new Date().getFullYear();
    let mm = new Date().getMonth()+1;
    let dd = new Date().getDate();
    let hh = new Date().getHours();
    let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
    let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
    _this.gettime = yy+'-'+mm+'-'+dd+' '+hh+':'+mf+':'+ss;
    this.Samplingtime=_this.gettime;
    console.log(_this.gettime)  
    }
}
}

获取文章流量推荐曝光度随便胡说八道两句

vue3.0带来了什么

1.性能的提升

  • 打包大小减少41%
  • 初次渲染快55%,更新渲染块133%
  • 内存减少54%
  • ........

2.源码的升级

  • 使用Proxy代替defineProperty实现响应式
  • 重写虚拟DOM的实现和Tree-Sharking
  • ......

3.拥抱TypeScript

  • vue3.0更好的支持TypeScript

4.新的特性

Composition API(组合api)

。 setup配置

。ref与reactive

。watch与watchEffect

。 provide和inject

。 .......

新的内置组件

。 Fragment

。Teleport

。Suspense

其他改变

。新的生命周期钩子

。data选项应始终被声明为一个函数

。移除keyCode支持作为v-on的修饰符

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

情绪员Tim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值