Vue3 Options API下如何触发日期选择器显示占位符dd.mm.yyyy
解决方法
你需要将绑定的日期值设置为空字符串(''),而不是undefined——因为input元素的占位符仅在其value为空字符串时才会显示,你的格式化逻辑对undefined的处理可能导致视图没有正确更新。
具体操作步骤:
- 在触发“恢复占位符”的操作(比如按钮点击事件)中,直接给绑定的
date变量赋值空字符串:resetDate() { this.date = ''; } - 同时确保你的
formatDate方法能正确处理空值,避免残留格式化痕迹:formatDate(e) { const inputValue = e.target.value; if (!inputValue) { this.date = ''; return; } // 保留原有的日期格式化逻辑 const cleaned = inputValue.replace(/\D/g, ''); const formatted = cleaned.length > 2 ? `${cleaned.slice(0,2)}-${cleaned.slice(2,4)}-${cleaned.slice(4,8)}` : cleaned; this.date = formatted; }
这样设置后,日期选择器就会回到初始显示dd.mm.yyyy占位符的状态。
内容的提问来源于stack exchange,提问作者Amrmsmb
相关产品推荐
相关产品推荐

