使用Taro 2.x尝试同构SSR,报错`ReferenceError: window is not defined`



  • 示例代码如下

    import Taro, { PureComponent } from '@tarojs/taro';
    import { View } from '@tarojs/components';
    
    class Index extends PureComponent { 
        render() {
            <View className='test'>
           </View>
        }
    }
    

    报错信息:

    Users/Workspace/proj_test/node_modules/@tarojs/components/dist/index.js:268
    	if (!options.singleton && typeof options.singleton !== "boolean") options.singleton = isOldIE();
                                                                                           ^
    ReferenceError: window is not defined
        at /Users/Workspace/proj_test/node_modules/@tarojs/components/dist/index.js:200:46
        at module.exports (/Users/Workspace/proj_test/node_modules/@tarojs/components/dist/index.js:268:88)
       
    

    isOldIE这个方法能不能判断一下是否是浏览器环境再执行吖?

    环境信息:
    Taro v3.0.16

    Taro CLI 3.0.16 environment info:
    System:
    OS: macOS 10.15.7
    Shell: 5.7.1 - /bin/zsh
    Binaries:
    Node: 12.13.0 - /usr/local/bin/node
    npm: 6.14.8 - /usr/local/bin/npm
    npmPackages:
    @tarojs/cli: 2.0.4 => 2.0.4
    @tarojs/components: 2.0.4 => 2.0.4
    @tarojs/mini-runner: 2.0.4 => 2.0.4
    @tarojs/router: 2.0.4 => 2.0.4
    @tarojs/taro: 2.0.4 => 2.0.4
    @tarojs/taro-h5: 2.0.4 => 2.0.4
    @tarojs/webpack-runner: 2.0.4 => 2.0.4
    eslint-config-taro: 2.0.4 => 2.0.4
    eslint-plugin-taro: 2.0.4 => 2.0.4
    nerv-devtools: ^1.5.6 => 1.5.6
    nervjs: ^1.5.6 => 1.5.6
    react: ^16.7.0 => 16.7.0
    npmGlobalPackages:
    typescript: 4.0.3


登录后回复