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

Vue3组件转HTML计算阅读时长,vite构建后render方法失效求助

问题

我需要将Vue3组件转换为HTML,以此计算文章的预估阅读时长。我的文章以Markdown格式存储,目前使用vite-plugin-vue-markdown将Markdown转换为Vue组件。

开发环境下,我可以通过以下代码获取文本:

const dom = new DOMParser().parseFromString(element.default.render().children[0].children, 'text/html')
const innerText = dom.querySelector('body').innerText

但执行vite build构建后,程序不再导出.render()方法。

我尝试了以下代码,却得到错误输出:

  • 尝试代码:
console.log(new DOMParser().parseFromString(element.default, 'application/xml'))
  • 输出结果:
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
      <h3>
        This page contains the following errors:
      </h3>
      <div style="font-family:monospace;font-size:12px">
        error on line 1 at column 1: Document is empty
      </div>
      <h3>
        Below is a rendering of the page up to the first error.
      </h3>
    </parsererror>
  </body>
</html>

所有操作均在.js文件中进行,而非.vue文件。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 01:54:55