VuePress时间线页面报错问题

... 2025-7-10
  • VuePress
小于 1 分钟

注:

  • 使用 VuePress 版本为 V1
  • 在主题开发者 Mr.Hope 大佬的博客中并没有发现此类问题

有关文件:

  • index 文件 :vuepress-plugin-active-hash\lib\node\index.js

  • rootMixin 文件:vuepress-plugin-active-hash\lib\client\rootMixin.js


index 文件中定义:

ACTIVE_HASH_CONTAINER_SELECTOR: options.containerSelecter || ".theme-default-content",
1

rootMixin 文件 第 22 行,JavaScript 查找类名为.theme-default-content 选择器时,并没有判断此元素是否存在,而直接调用了 offsetTop 属性。


/timeline/ 页面中,没有检测到 index 文件 下的 options.containerSelecter

所以默认 ACTIVE_HASH_CONTAINER_SELECTOR.theme-default-content

但是在此页面中,并不存在类名为 .theme-default-content 的元素

导致打点调用 offsetTop 属性时报错

解决方法:

rootMixin 文件中,在查找元素时添加判断:

const defaultContent = document.querySelector(ACTIVE_HASH_CONTAINER_SELECTOR)
const themeContentScrollTop = defaultContent ? defaultContent.offsetTop : 0
1
2
上次编辑于: 2025年7月10日 04:01
贡献者: HugStars