【发布时间】:2021-01-29 22:43:25
【问题描述】:
我尝试构建我的 Gatsby 应用程序并返回此错误:
构建静态 HTML 失败
有关此错误的更多信息,请参阅我们的文档页面:https://gatsby.dev/debug-html
WebpackError: ReferenceError: Element is not defined
- bootstrap.bundle.min.js:6
node_modules/bootstrap/dist/js/bootstrap.bundle.min.js:6:3033
- bootstrap.bundle.min.js:6
node_modules/bootstrap/dist/js/bootstrap.bundle.min.js:6:68
- bootstrap.bundle.min.js:6
node_modules/bootstrap/dist/js/bootstrap.bundle.min.js:6:162
- Layout.js:1
src/components/Layout.js:1:1
- 404.js:1
src/pages/404.js:1:1
我已经通过 npm 安装了引导程序并将其包含在我的 Layout 组件中:
import React, { useEffect } from "react"
import "bootstrap/dist/js/bootstrap.bundle.min.js"
import "bootstrap/dist/css/bootstrap.min.css"
import "../assets/css/style.css"
import "../assets/css/aos.css"
import Navbar from '../components/Navbar'
import Footer from '../components/Footer'
郑重声明,我的应用在 gatbsy develop 上运行良好。它还在终端上显示以下奇怪的引导代码:
我不确定是什么原因造成的,但我保证引导程序在我的应用上运行良好。
我该如何解决这个问题?
更新:我通过将 ff 添加到我的 gatsby 浏览器来解决问题 WebpackError: ReferenceError: Element is not defined:
import "bootstrap/dist/css/bootstrap.min.css"
import "bootstrap/dist/js/bootstrap.bundle.min.js"
但后来我得到一个新的错误(完整的日志):
Building static HTML failed for path "/blogs/react-js-why"
See our docs page for more info on this error: https://gatsby.dev/debug-html
6 | }
7 | if (isProduction) {
> 8 | throw new Error(prefix);
| ^
9 | }
10 | throw new Error(prefix + ": " + (message || ''));
11 | }
WebpackError: Invariant failed
- tiny-invariant.esm.js:8
node_modules/tiny-invariant/dist/tiny-invariant.esm.js:8:1
- history.js:250
- history.js:250
node_modules/history/esm/history.js:250:115
- react-router-dom.js:29
node_modules/react-router-dom/esm/react-router-dom.js:29:41
经过一番研究,结果指向了react-router-dom在盖茨比中的使用。不确定是否是问题,但我在 PageLinks 组件上使用了它:
import React from "react"
import { BrowserRouter as Router, Switch } from "react-router-dom"
import { HashLink } from 'react-router-hash-link'
import Scrollspy from 'react-scrollspy'
const data = [
{
id: 1,
text: "Home",
div: "home",
url: "#home"
},
{
id: 2,
text: "About",
div: "about",
url: "#about"
},
{
{
id: 3,
text: "Blogs",
div: "blogs",
url: "#blogs",
},
{
id: 4,
text: "Contact",
div: "contact",
url: "#contact"
},
]
let divs = data.map(x => x.div)
export default () => {
return (
<Router>
<Switch>
<Scrollspy className="navbar-nav ml-auto" items={ divs } currentClassName="active">
{ data.map(link => {
return (
<li className="nav-item" key={link.id}>
<HashLink smooth to={link.url} className="nav-link text-uppercase font-weight-bold">{link.text}</HashLink>
</li>
)
})
}
</Scrollspy>
</Switch>
</Router>
)
}
【问题讨论】:
-
您能否解释或详细说明“引导程序在我的应用程序上运行良好”?该问题是由 Bootstrap 或其实现引起的
-
@FerranBuireu 解决了这个问题,但我收到了一个新错误
WebpackError: Invariant failed,真是太好了 -
您可以通过添加正确的日志和相关信息来编辑整个问题吗?
-
你在那里使用任何
<Link>组件吗? -
我在我的徽标链接上更新了我在 gatsby
import { Link } from 'gatsby'上使用链接的问题
标签: reactjs twitter-bootstrap gatsby