iview/src/components/time/time.vue

68 lines
1.7 KiB
Vue
Raw Normal View History

2018-05-17 17:51:37 +08:00
<template>
2018-05-17 18:06:42 +08:00
<span :class="classes" @click="handleClick">time</span>
2018-05-17 17:51:37 +08:00
</template>
<script>
2018-05-17 18:24:26 +08:00
import Vue from 'vue';
const isServer = Vue.prototype.$isServer;
2018-05-17 18:06:42 +08:00
import { oneOf } from '../../utils/assist';
const prefixCls = 'ivu-time';
2018-05-17 17:51:37 +08:00
2018-05-17 18:06:42 +08:00
export default {
name: 'Time',
props: {
time: {
type: [String, Number, Date],
required: true
},
type: {
type: String,
validator (value) {
return oneOf(value, ['relative', 'date', 'datetime']);
},
default: 'relative'
},
hash: {
type: String,
default: ''
},
interval: {
type: Number,
default: 60
}
},
data () {
return {
date: ''
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-with-hash`]: this.hash
}
];
}
},
methods: {
handleClick () {
if (this.hash !== '') window.location.hash = this.hash;
2018-05-17 18:24:26 +08:00
},
setTime () {
2018-05-17 18:06:42 +08:00
}
},
mounted () {
this.setTime();
2018-05-17 18:24:26 +08:00
if (isServer) return;
2018-05-17 18:06:42 +08:00
this.timer = setInterval(() => {
this.setTime();
}, 1000 * this.interval);
},
beforeDestroy () {
if (this.timer) clearInterval(this.timer);
}
2018-05-17 17:51:37 +08:00
};
</script>