[英]Vue.js Server Side Rendering: document is not defined
I am using a package which uses document
and window
. 我正在使用一个使用document
和window
的包。 I want to make my website have server-side rendering without changing too much code. 我想让我的网站具有服务器端渲染,而无需更改太多代码。 For window is not defined
, I use window-or-global
( https://www.npmjs.com/package/window-or-global ) and it solves my problem. 对于window is not defined
,我使用window-or-global
( https://www.npmjs.com/package/window-or-global )并解决了我的问题。
However, when it comes to document
, I actually don't find anything useful. 但是,当谈到document
,我实际上找不到任何有用的东西。 My idea is try to find out when the server or the client is rendering. 我的想法是尝试找出服务器或客户端何时呈现。 I will only execute the code with document
when the client is rendering. 我只会在客户端渲染时用document
执行代码。 Is there any way to do that? 有没有办法做到这一点?
P/S: I am using Vue.js 2.0. P / S:我使用的是Vue.js 2.0。 The package I am using is fine-uploader ( https://www.npmjs.com/package/fine-uploader ) 我正在使用的包是精品上传器( https://www.npmjs.com/package/fine-uploader )
I'm also using Vue Server Side rendering in a new project, and what I do for this kind of problem is that I check if I am in the browser environment or in the server one using: 我也在一个新项目中使用Vue Server Side渲染,我为这类问题做的是检查我是在浏览器环境中还是在服务器端使用:
if (typeof window !== 'undefined') {
// We are in the browser
}
You can use 您可以使用
const Vue = require('vue');
Vue.prototype.$isServer
or from within a component 或来自组件内部
this.$isServer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.