Element: 用vue-server-renderer加载,require后报错ReferenceError: window is not defined.

Created on 11 Oct 2016  ·  7Comments  ·  Source: ElemeFE/element

ElementUI version

OS/Browers version

Vue version

Reproduction Link

Steps to reproduce

What is Expected?

What is actually happening?

discussion

Most helpful comment

报 window is not defined 大多数是用了 style-loader 这个库
今天调了一天 终于发现这个问题了 把style-loader去掉就好了

All 7 comments

支持 SSR 应该要从开始做的时候就支持,要根据环境写不同的代码,这块 @jiyinyiyong 应该比较熟悉。

一般前端都会用到大量的 window 的, 不好直接干掉. 现在应该说没有完美支持.
官方提到的做法有一些:

我估计上边两个办法都是不理想的, 还是需要源码当中对 window 对象做检测之类的.

简单说就是现在还不适合, 搞起来挺麻烦...

好像看到过 vuex 源码里有做检测 当时没在意

暂不计划支持 SSR。

报 window is not defined 大多数是用了 style-loader 这个库
今天调了一天 终于发现这个问题了 把style-loader去掉就好了

const { JSDOM } = require('jsdom')
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', { url: 'http://localhost:8080' })
if (typeof window === 'undefined') {
  global.window = dom.window
  global.document = window.document
  global.navigator = window.navigator
}

将style-loader改为vue-style-loader

Was this page helpful?
0 / 5 - 0 ratings