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
   }
}
相关推荐
朝阳3919 分钟前
浏览器【详解】requestIdleCallback(浏览器空闲时执行)
前端·浏览器
典学长编程27 分钟前
前端开发(HTML,CSS,VUE,JS)从入门到精通!第二天(CSS)
前端·javascript·css·html
Chase_______40 分钟前
JavaWeb笔记2-JavaScript&Vue&Ajax
开发语言·javascript·vue.js
丘山子44 分钟前
DNS 原理入门
前端·后端·面试
2501_916007471 小时前
iPhone查看App日志和系统崩溃日志的完整实用指南
android·ios·小程序·https·uni-app·iphone·webview
Kyle19941 小时前
RollCode:高效低代码开发新体验
前端
这是个栗子1 小时前
【Node.js安装注意事项】-安装路径不能有空格
前端·npm·node.js
源力祁老师1 小时前
外部系统获取Odoo数据最便捷的方式
开发语言·前端·javascript
用户9714171814271 小时前
picker-view选中框不居中
前端
YGY_Webgis糕手之路1 小时前
Cesium 快速入门(十) JulianDate(儒略日期)详解
前端·gis·cesium