Vue3项目中如何动态切换Flatpickr主题?
在Vue 3中实现Flatpickr动态切换主题的问题
我在Vue 3项目中使用flatpickr,希望实现用户动态切换主题的功能。最初通过以下代码将主题设置为material_blue:
require("flatpickr/dist/themes/material_blue.css");
但后续切换主题时,比如执行:
require("flatpickr/dist/themes/material_red.css");
之前加载的CSS文件仍会保留(推测是缓存导致),新主题无法生效。尝试过“卸载模块”(unrequiring)的方式也没有成功。
已实现的解决方案
1. 初始化默认主题
在项目的index文件中,通过<link>标签引入默认主题样式:
<link rel="stylesheet" type="text/css" href="https://npmcdn.com/flatpickr/dist/themes/dark.css" />
2. 动态切换主题逻辑
当用户触发主题变更操作时,直接修改对应<link>标签的href属性:
var stylesheet = document.head.querySelector("link[href*='themes']"); stylesheet.href = "https://cdn.jsdelivr.net/npm/flatpickr/dist/themes/" + myNewTheme + ".css";
内容的提问来源于stack exchange,提问作者RGriffiths
相关产品推荐
相关产品推荐

