美文网首页
2020-01-20 基于Vue Composition API

2020-01-20 基于Vue Composition API

作者: rub1cky | 来源:发表于2020-01-20 14:37 被阅读0次
    import { DatePicker } from 'ant-design-vue'
    import moment from 'moment'
    import { reactive, toRefs, watch } from '@vue/composition-api'
    
    export default {
      name: 'OleDatePicker',
      model: {
        prop: 'value',
        event: 'change'
      },
      props: {
        value: String
      },
      setup (props, { attrs, emit }) {
        const state = reactive({ dateValue: null })
    
        let { showTime } = attrs
    
        watch(() => props.value, v => {
          state.dateValue = v ? moment(v) : null
        })
    
        watch(() => state.dateValue, (v) => {
          const format = showTime ? `YYYY-MM-DD HH:mm:ss` : `YYYY-MM-DD`
          let dateEmit = v ? moment(v).format(format) : null
          emit('change', dateEmit)
        })
    
        const handleChange = (date) => {
          state.dateValue = date
        }
    
        return {
          ...toRefs(state),
          handleChange
        }
      },
      render () {
        const props = {
          attrs: this.$attrs,
          props: {
            ...this.$attrs,
            value: this.dateValue
          },
          on: {
            ...this.$listeners,
            change: this.handleChange
          }
        }
        return <DatePicker {...props}></DatePicker>
      }
    }
    

    相关文章

      网友评论

          本文标题:2020-01-20 基于Vue Composition API

          本文链接:https://www.haomeiwen.com/subject/apujzctx.html