【发布时间】:2018-02-23 07:24:32
【问题描述】:
我正在使用 Gatsby 并导入 jquery。
当我运行Gatsby build 时,出现以下错误:
WebpackError: jQuery 需要一个带有文档的窗口。
这是由于 Gatsby 进行服务器端渲染。
我已经阅读了 GitHub 上的许多问题(this one 是我能找到的最好的问题)。
我的代码如下所示:
import React, { Component } from 'react'
import Link from 'gatsby-link'
import LandingScreen from '../components/LandingScreen'
import $ from 'jquery'
import 'fullpage.js/dist/jquery.fullPage.js'
import 'fullpage.js/dist/jquery.fullpage.css'
class TestPage extends Component {
componentDidMount() {
$('#fullpage').fullpage({
verticalCentered: false
});
}
render(){
return (
<main id="fullpage">
<LandingScreen />
</main>
)
}
}
export default TestPage
这很糟糕,所以我根据上面的 GitHub 线程尝试了以下操作,但这也失败了:
if (typeof window !== 'undefined') {
import $ from 'jquery'
}
谁能建议如何导入jquery?
【问题讨论】:
标签: javascript reactjs webpack gatsby