uniapp 组件获取两个异步值确保两个值均存在

uniapp在封装组件时需要传递两个异步获取的值,当两个值均存在时需要进行编辑回显操作,

例如如下的一个组件:

javascript 复制代码
<template>
  <view class="picker-comp">
    <picker :range="range" :range-key="rangeKey" :value="innerValue" @change="handleChange" :disabled="isDisabled">
      <view class="text-content">
        <text v-if="text" :style="{opacity: isDisabled ? '0.6' : '1'}">{{text}}</text>
        <text v-else>{{ placeholder }}</text>
        <!-- <slot></slot> -->
      </view>
      
    </picker>
  </view>
</template>

<script>
  export default {
    props: {
      isDisabled: {
        type: Boolean,
        default: false
      },
      range: {
        type: Array,
        required: true
      },
      rangeKey: {
        type: String,
        default: ''
      },
      rangeValue: {
        type: String,
        default: ''
      },
      value: {
        type: [Number, String],
        default: ''
      },
      placeholder: {
        type: String,
        default: '请选择'
      }
    },
    computed: {
      bothValuesExist() {
        if(this.value && this.range && this.range.length) {
          return true
        } else {
          return false
        }
      }
    },
    watch: {
      bothValuesExist: {
        handler(newVal) {
          if(newVal) {
            if(this.rangeValue) {
              const obj = this.range.find(item => item[this.rangeValue] == this.value)
              this.text = obj && obj[this.rangeKey]
            } else {
              this.text = this.value
            }
            
          }
        },
        immediate: true
      }
    },
    data() {
      return {
        innerValue: '',
        text: '',
      }
    },
    methods: {
      handleChange(e) {
        const newValue = e.mp.detail.value;
        this.innerValue = newValue;
        if(this.rangeValue) {
          this.$emit('input', this.range[newValue][this.rangeValue]);
          this.text = this.range[newValue][this.rangeKey]
        } else {
          this.$emit('input', this.range[newValue]);
          this.text = this.range[newValue]
        }
      }
    },
  }
</script>

<style lang="scss" scoped>
  .text-content {
    text-align: right;
    font-size: 16px;
    color: #666;
    padding: 0 10px;
    text {
      padding: 0 8px;
    }
  }
</style>

应为computed只要值发生改变会重新计算新值,所以我们直接在computed内部将两个异步值进行判断,最后watch计算后的新值即可。

核心代码

javascript 复制代码
computed: {
  bothValuesExist() {
    if(this.value && this.range && this.range.length) {
      return true
    } else {
      return false
    }
  }
},
watch: {
  bothValuesExist: {
    handler(newVal) {
      if(newVal) {
        if(this.rangeValue) {
          const obj = this.range.find(item => item[this.rangeValue] == this.value)
          this.text = obj && obj[this.rangeKey]
        } else {
          this.text = this.value
        }
        
      }
    },
    immediate: true
  }
},

参考代码

javascript 复制代码
computed:{
   tempA3(){
       const {a1,a2} = this
       retutn {a1,a2}
   }
},
watch:{
   tempA3:{
       handler(val){
           const {a1,a2} = val
           if(a1 && a2){
               this.initA3()
           }
       },
       deep: true
   }
}
相关推荐
还是鼠鼠1 小时前
图书管理系统 Axios 源码 __删除图书功能
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
轻口味2 小时前
Vue.js `Suspense` 和异步组件加载
前端·javascript·vue.js
m0_zj3 小时前
8.[前端开发-CSS]Day08-图形-字体-字体图标-元素定位
前端·css
还是鼠鼠3 小时前
图书管理系统 Axios 源码__编辑图书
前端·javascript·vscode·ajax·前端框架
北极象3 小时前
vue3中el-input无法获得焦点的问题
前端·javascript·vue.js
百度网站快速收录3 小时前
网站快速收录:如何优化网站头部与底部信息?
前端·html·百度快速收录·网站快速收录
Loong_DQX4 小时前
【react+redux】 react使用redux相关内容
前端·react.js·前端框架
GISer_Jing4 小时前
react redux监测值的变化
前端·javascript·react.js
engchina4 小时前
CSS 样式化表格:从基础到高级技巧
前端·css
m0_528723814 小时前
react中useEffect的使用
前端·javascript·react.js