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

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

组件:u-input

html 复制代码
<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 |

使用组件:

html 复制代码
<!-- 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"
/>
相关推荐
山雀~2 小时前
CSP 安全漏洞修复
前端·网络协议
陌离3 小时前
Go-Gui:一套 无限接近 现代化UI的 GPU绘制界面组件框架
前端
咚咚咚小柒3 小时前
【前端】LangChain.js学习
前端·javascript·人工智能·typescript·langchain·vue·ai编程
2401_885885043 小时前
巴林语音通知接口如何对接?系统告警语音API接入教程
android·前端·网络·python·语音识别
java1234_小锋3 小时前
【技术专题】CSS3 网页技术 - CSS3 盒模型
前端·css·css3
计算机魔术师3 小时前
手握芯片、又要吞下开源分发入口,英伟达被盯上了
前端
段一凡-华北理工大学4 小时前
高炉能源系统智能化:热风炉、煤气与极致能效~系列文章07:高炉煤气:产生规律与全厂用户平衡
java·前端·能源
前端架构师-老李4 小时前
HTML与CSS基础第四课—strong、em、b、i 各表示什么
前端·css·html
king王一帅5 小时前
AI 时代一群人在卷笔记软件,我想先把 Markdown 编辑器的底层做好
前端·ai编程·markdown
用户11481867894845 小时前
盲水印:DCT-QIM 从原理到踩坑
前端