当前位置: 首页 > news >正文

修改Uniapp input 组件不刷新视图问题

uni-app 的<input>是原生组件,外部修改 value 不会刷新视图,改 key 强制销毁重建

组件:u-input

<template> <view class="input-wrapper"> <!-- 用 key 强制重新渲染,解决 uni-app input 值不同步的问题 --> <input class="uni-input" :key="inputKey" :placeholder="placeholder" :value="value" :type="inputType" :password="showPassword" @input="handleInput" v-bind="$attrs" /> <uni-icons v-if="showClearIcon" type="close" class="uni-icon" :size="iconSize" :color="iconColor" @click="handleClearValue" /> <uni-icons v-if="isPassWord" class="uni-icon" :size="iconSize" :color="iconColor" @click="changePassword" :type="showPassword ? 'eye-filled' : 'eye-slash-filled'" /> </view> </template> <script> export default { name: "u-input", inheritAttrs: false, props: { value: { type: [String, Number], default: "", }, type: { type: String, default: "text", }, placeholder: { type: String, default: "请输入", }, iconSize:{ type:Number, default:20 }, iconColor:{ type:String, default:"#808080" } }, data() { return { showPassword: false, showClearIcon: false, inputKey: 0, // 用于强制刷新 input }; }, computed: { isPassWord() { return this.type === "password" || this.type === "safe-password"; }, inputType(){ this.showPassword = this.isPassWord return this.isPassWord?"":this.type } }, watch: { // 监听 value 变化,保持清除按钮状态同步 value: { immediate: true, handler(val) { this.showClearIcon = val && val.length > 0; }, }, }, methods: { handleInput(event) { const val = event.detail.value; this.$emit("input", val); this.showClearIcon = val.length > 0; }, handleClearValue() { this.$emit("input", ""); this.showClearIcon = false; this.inputKey++; }, changePassword() { this.showPassword = !this.showPassword; }, }, }; </script> <style scoped lang="scss"> .input-wrapper { display: flex; flex-direction: row; align-items: center; width: 100%; } .uni-input { flex: 1; width: 100%; } .uni-icon{ margin: 0 $uni-spacing-row-sm; } </style>
inheritAttrs: false阻止$attrs自动绑定到组件根元素<view>
v-bind="$attrs"绑定到<input>未在 props 中声明的属性全部透传给原生 input

使用组件:

<!-- placeholder、maxlength、disabled 等全部透传 --> <u-input v-model="form.username" placeholder="请输入用户名" maxlength="20" :disabled="isDisabled" confirm-type="done" /> <u-input v-model="form.password" type="password" placeholder="请输入密码" maxlength="32" />
http://www.jsqmd.com/news/1152149/

相关文章:

  • Windsurf vs Cursor谁更适合你?——基于127个真实团队调研、86小时编码追踪与LLM上下文理解准确率92.4%的权威结论
  • openeuler/prefetch_tuning常见问题解答:编译错误、参数失效与系统兼容性解决方案
  • Agent越学越像在重新理解操作系统
  • 提示词泛化性不足?Few-shot Learning的7类语义坍缩陷阱,及4种对抗性构造法
  • TongWeb7容器版 无法动态注册servelet
  • 软件测试报告一般应用在哪些场景?中承信安专业科普
  • 双工位蜡镶机选型技术指南:五大量化指标与厂家梯队对比分析
  • DDrawCompat:现代Windows系统下的DirectDraw兼容性解决方案
  • 东北自建房墙面抹灰用哪种砂浆不容易开裂?佳木斯本地砂浆怎么选?
  • 基于《人工智能 智能体互联》国标的 AIP 开源项目在 AtomGit 正式开源
  • 5分钟终极指南:用Unlock-Music轻松解锁加密音乐文件
  • 装修中古风亮灯效果拉满[特殊字符]
  • 遐思心智 (01):万古更新,AI在此
  • 企业出海,为什么越来越离不开住宅IP?
  • 如何用 ClaudeAPI 总结游客反馈,并反过来优化旅游线路
  • 用百考通写出一份有底气的任务书 ✍️
  • 徐州企业如何选型订货系统?本地服务商深度对比指南
  • 为什么要学习 USB 协议
  • 2026年开发小程序公司有哪些,模板与定制服务商汇总
  • 去虚向实、聚力深耕!
  • 告别手动复制粘贴!用 RPA 微信自动化接口实现社群高效管理
  • 开源金融 Agent 横评,LangAlpha 与 TradingAgents 的路线之争
  • 深入解析HotSpot VM源码,Java程序员进阶必备!
  • AI学习回路:基础模型同质化时代的企业核心竞争力构建
  • 终极Photoshop AI插件SD-PPP:5分钟让你的设计工作流智能化
  • 2026年健康新风尚:探秘苦荞快餐粉背后的匠心制造
  • 影刀RPA Excel数据清洗实战:空行、重复、乱码、格式混乱的处理
  • Stable Diffusion批量生成商品主图:单日产出2000+合规图,中小卖家必须掌握的3个LoRA微调技巧
  • TikTok爆款搬运与二创全链路:从无水印提取到AI去重复刻,这套工具组合让产能翻倍
  • [TradeAI] Kario 系统启动与运维指南