美文网首页
2019-12-14 基于Vue Composition API

2019-12-14 基于Vue Composition API

作者: rub1cky | 来源:发表于2020-01-20 11:30 被阅读0次

    为减少loading态的重复代码,利用新API封装

    import { watch, reactive, toRefs } from '@vue/composition-api'
    
    export default {
      name: 'VPromise',
      props: {
        promise: {
          validator: p =>
            p && typeof p.then === 'function' && typeof p.catch === 'function'
        }
      },
      setup(props) {
        const promise = props.promise
    
        const state = reactive({
          resolved: false,
          data: null,
          error: null
        })
    
        watch(
          () => promise,
          p => {
            if (p) {
              p.then(data => {
                if (promise === p) {
                  state.data = data
                  state.resolved = true
                }
              }).catch(err => {
                if (promise === p) {
                  state.error = err
                  state.resolved = true
                }
              })
            }
          }
        )
    
        return {
          ...toRefs(state)
        }
      },
      render(h) {
        let slots = this.$scopedSlots
    
        if (this.error) {
          return h('span', slots['error'](this.error))
        }
    
        if (this.resolved) {
          return h('span', slots['resolved'](this.data))
        }
    
        return h('span', slots['pending'](this.data))
      }
    }
    
    

    模版中使用

    <promise :promise="promise">
          <template v-slot:pending>
            <span>loading</span>
          </template>
          <template v-slot:resolved="data">
            <span>{{data}}</span>
          </template>
          <template v-slot:error>
            <span>render-error</span>
          </template>
        </promise>
    

    其中promise 传入一个promise对象

    相关文章

      网友评论

          本文标题:2019-12-14 基于Vue Composition API

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