封装uview2的picker组件(uniapp)

1.源码

复制代码
<template>
  <view>
    <view :class="[getPickerName ? '' : 'is-placeholder']" @click="onShowPicker">{{ getPickerName || placeholder }}</view>
    <u-picker v-if="showStatus" :show="show" :columns="columns" :key-name="keyName" @cancel="show = false" @confirm="onConfirm" />
  </view>
</template>

<script>
export default {
  name: 'HgPicker',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    columns: {
      type: Array,
      default() {
        return []
      }
    },
    keyName: {
      type: String,
      default: 'label'
    },
    placeholder: {
      type: [String, Number],
      default: '请选择'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      show: false,
      showStatus: true
    }
  },
  computed: {
    getPickerName() {
      if (this.columns.length) {
        for (let item of this.columns[0]) {
          if (item.id === this.value) {
            return item[this.keyName]
          }
        }
      }
      return ''
    }
  },
  methods: {
    onShowPicker() {
      if (!this.disabled) {
        this.show = true
      }
    },
    onConfirm({ indexs, value, values }) {
      this.$emit('input', value[0].id)
      this.$emit('change', value[0].id)
      this.$emit('changes', indexs[0])
      this.show = false
    },
    reload() {
      this.showStatus = false
      this.$nextTick(() => {
        this.showStatus = true
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.is-placeholder {
    color: #b5afaf;
}
</style>

2.使用方法

<hg-picker v-model="formModel.mode" :columns="capacityModeList" style="flex:1" />

相关推荐
lichenyang4533 小时前
Docker 学习笔记(一):为什么需要镜像、容器和仓库?
前端
kyriewen3 小时前
别再对着 TypeScript 报错发呆了:我把 10 个最常见的红色波浪线翻译成了人话
前端·javascript·typescript
IT_陈寒3 小时前
SpringBoot自动配置的坑,我的API突然就404了
前端·人工智能·后端
free354 小时前
从 0 实现一个 Tiny JavaScript VM:项目架构拆解
javascript
奇奇怪怪的4 小时前
Embedding 模型 10+ 横向评测
前端
陈广亮4 小时前
Monorepo 从 0 到 1 实操指南 2026 版:pnpm catalogs + Turborepo 2.x + changesets 全链路
前端
子兮曰4 小时前
OpenMontage 深度解剖:你的 AI 编程助手,其实是个视频工作室
前端·后端·ai编程
敲代码的鱼4 小时前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
子兮曰4 小时前
前端工具链的「Rust 化」:一场没有赢家的军备竞赛?
前端·后端·rust
Hyyy5 小时前
Function Calling / Tool Use的原理和实现模式
前端·llm·ai编程