gaoluyang
3 天以前 92230c9a97dc9ce9df3313d11d26999c04bb6b26
src/uni_modules/uni-dateformat/components/uni-dateformat/uni-dateformat.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,88 @@
<template>
   <text>{{dateShow}}</text>
</template>
<script>
   import {friendlyDate} from './date-format.js'
   /**
    * Dateformat æ—¥æœŸæ ¼å¼åŒ–
    * @description æ—¥æœŸæ ¼å¼åŒ–组件
    * @tutorial https://ext.dcloud.net.cn/plugin?id=3279
    * @property {Object|String|Number} date æ—¥æœŸå¯¹è±¡/日期字符串/时间戳
    * @property {String} locale æ ¼å¼åŒ–使用的语言
    *    @value zh ä¸­æ–‡
    *    @value en è‹±æ–‡
    * @property {Array} threshold åº”用不同类型格式化的阈值
    * @property {String} format è¾“出日期字符串时的格式
    */
   export default {
      name: 'uniDateformat',
      props: {
         date: {
            type: [Object, String, Number],
            default () {
               return '-'
            }
         },
         locale: {
            type: String,
            default: 'zh',
         },
         threshold: {
            type: Array,
            default () {
               return [0, 0]
            }
         },
         format: {
            type: String,
            default: 'yyyy/MM/dd hh:mm:ss'
         },
         // refreshRate使用不当可能导致性能问题,谨慎使用
         refreshRate: {
            type: [Number, String],
            default: 0
         }
      },
      data() {
         return {
            refreshMark: 0
         }
      },
      computed: {
         dateShow() {
            this.refreshMark
            return friendlyDate(this.date, {
               locale: this.locale,
               threshold: this.threshold,
               format: this.format
            })
         }
      },
      watch: {
         refreshRate: {
            handler() {
               this.setAutoRefresh()
            },
            immediate: true
         }
      },
      methods: {
         refresh() {
            this.refreshMark++
         },
         setAutoRefresh() {
            clearInterval(this.refreshInterval)
            if (this.refreshRate) {
               this.refreshInterval = setInterval(() => {
                  this.refresh()
               }, parseInt(this.refreshRate))
            }
         }
      }
   }
</script>
<style>
</style>