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

Vue3项目中Vanilla JS Datepicker的正确配置方法咨询

在Vue3项目中配置Vanilla JS Datepicker的正确方式

1. 安装依赖

首先执行包安装命令:

npm install --save-dev vanillajs-datepicker

2. 初始尝试的问题

最初的模板和脚本代码如下:
模板:

<input type="text" name="foo" />

脚本:

import { Datepicker } from "vanillajs-datepicker";

mounted() {
    const elem = document.getElementById("foo");
    const datepicker = new Datepicker(elem, {
      // ...配置项
    });
},

此时出现ESLint报错:

error 'datepicker' is assigned a value but never used

同时在Vue项目中直接使用document.getElementById并非最佳实践,Vue提供了更可靠的元素引用方案。

3. 修复后的正确配置

更新模板使用ref标识目标元素:

<input type="text" ref="foo" />

脚本中通过this.$refs获取元素并实例化Datepicker(若无需后续操作实例,可直接实例化不赋值变量,避免未使用变量的报错):

import { Datepicker } from "vanillajs-datepicker";

mounted() {
    new Datepicker(this.$refs.foo, {
      // ...配置项
    });
},

调整后配置即可正常运行。

内容的提问来源于stack exchange,提问作者RGriffiths

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 00:20:44