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
   }
}
相关推荐
摸鱼的春哥13 分钟前
前端程序员最讨厌的10件事
前端·javascript·后端
牧羊狼的狼4 小时前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手6 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
luckys.one6 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
魔云连洲6 小时前
深入解析:Vue与React的异步批处理更新机制
前端·vue.js·react.js
mCell6 小时前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
weixin_437830948 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
超级无敌攻城狮8 小时前
3 分钟学会!波浪文字动画超详细教程,从 0 到 1 实现「思考中 / 加载中」高级效果
前端
excel8 小时前
用 TensorFlow.js Node 实现猫图像识别(教学版逐步分解)
前端
gnip9 小时前
JavaScript事件流
前端·javascript