You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:37:04