使用jsdom解决第三方插件库不支持SSR所带来的新问题! #931
-
在vuepress.config.js的头部我增加了以下代码,第三方插件库不再报找不到window下面变量的错误了。 const jsdom = require("jsdom");
const {JSDOM} = jsdom;
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>')
if (typeof window === 'undefined') {
global.window = dom.window;
global.document = dom.window.document;
global.navigator = dom.window.navigator;
global.location = dom.window.location;
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
d-comp\node_modules\sass\sass.dart.js |
Beta Was this translation helpful? Give feedback.
-
@Mister-Hope 大佬可以帮忙看一下吗,之前我在issues中提交过类似的问题 |
Beta Was this translation helpful? Give feedback.
-
不是,我都没搞清楚你在干什么?
对于所有涉及操作 DOM 的库,你都应该在 Client-Only 周期或仅在客户端侧挂载的组件中动态载入。换而言之,只有在用户浏览器上,VuePressApp 挂载之后,你才应该导入并解析这些库,之后调用对应方法更新DOM。 听懂了么? |
Beta Was this translation helpful? Give feedback.
-
首先表示感谢,今天终于把问题解决了。
|
Beta Was this translation helpful? Give feedback.
不是,我都没搞清楚你在干什么?
对于所有涉及操作 DOM 的库,你都应该在 Client-Only 周期或仅在客户端侧挂载的组件中动态载入。换而言之,只有在用户浏览器上,VuePressApp 挂载之后,你才应该导入并解析这些库,之后调用对应方法更新DOM。
听懂了么?