vue-cli项目中登录信息包括登录时间,实现右上角显示实时时间
<template>
<div class="login-box">
<span>{{userData.loginDate}}</span>
</div>
</template>
<script>
export default {
name: "user-info",
data() {
return {
userData: {
loginDate:""
}
}
},
methods: {
//获取当前日期
getNowDate() {
let date = new Date(),
Y = date.getFullYear()+'-',
M = (date.getMonth()+1<10?'0'+(date.getMonth()+1):date.getMonth()+1)+'-',
D = (date.getDate()<10?'0'+date.getDate():date.getDate()+'-',
h = (date.getHours()<10?'0'+date.getHours():date.getHours()+'-',
m = (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()+'-',
s = (date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds()+'-',
this.userData.loginDate = Y+M+D+h+m+s;
console.log(this.userData.loginDate)
}
}
mounted:function(){
setInterval(this.getNowDate,1000)//实时刷新
}
}
</script>
打印结果:
2018-08-14 15:54:33
2018-08-14 15:54:34
2018-08-14 15:54:35
2018-08-14 15:54:36
。。。