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
   }
}
相关推荐
隔壁的大叔12 小时前
由于vite版本不一致,导致vue组件引入报错
javascript·vue.js
申阳12 小时前
Day 11:集成百度统计以监控站点流量
前端·后端·程序员
2501_9159090612 小时前
深入理解HTTPS和HTTP的区别、工作原理及安全重要性
安全·http·ios·小程序·https·uni-app·iphone
Cache技术分享12 小时前
239. Java 集合 - 通过 Set、SortedSet 和 NavigableSet 扩展 Collection 接口
前端·后端
超级罗伯特12 小时前
大屏自适应,响应式布局,亲测有效
前端·javascript·html·大屏·驾驶舱
青衫码上行12 小时前
【Java Web学习 | 第九篇】JavaScript(3) 数组+函数
java·开发语言·前端·javascript·学习
前端老宋Running12 小时前
React组件命名为什么用小写开头会无法运行?
前端·react.js·面试
百***071812 小时前
WebSpoon9.0(KETTLE的WEB版本)编译 + tomcatdocker部署 + 远程调试教程
前端
ruanCat12 小时前
对 changelogen 和 changelogithub 使用的思考
前端·github
前端Hardy12 小时前
HTML&CSS&JS:赛博木鱼
前端·javascript·css