javascript - 导航栏按钮在 Gatsby 和 TypeScript 的生产环境中不起作用
问题描述
我的项目的堆栈是 GatsbyJS、TypeScript、Twin Macro (TailwindCSS) 和 Emotion。
在我的styles.tsx文件中,我设计并定义了我的导航栏按钮,如下所示:
import React, {FC} from 'react'
import tw, {styled} from 'twin.macro'
export type ButtonComponentProps = {
children?: any,
onClick?: (e?: React.MouseEvent) => void
} & React.ButtonHTMLAttributes<HTMLButtonElement>
const ThisButton = ({onClick, children, ...rest}: ButtonComponentProps) =>
<button onClick={onClick} {...rest}>{children}</button>
export const NavButton = tw(ThisButton)`
text-black cursor-pointer text-xl leading-none px-3 py-1 border border-solid
border-transparent rounded bg-transparent block lg:hidden outline-none
focus:outline-none
`
现在我的标记文件index.tsx看起来像这样:
import React, {useState} from 'react'
import {faBars} from '@fortawesome/free-solid-svg-icons'
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'
import {Nav, InnerNav, InnerMost, DropDown, Logo, NavButton,
DropMenu, ListItem, InnerItem, ExtraButton
} from './styles'
import {StaticImage} from 'gatsby-plugin-image'
const Navbar = () => {
const [navbarOpen, setNavbarOpen] = useState(false)
return (
<>
<Nav>
<InnerNav>
<InnerMost>
<Logo href="/">
<StaticImage
src="../../images/logo.png"
alt="Company logo"
width={180}
placeholder="blurred"
/>
</Logo>
<NavButton
type="button"
onClick={() => setNavbarOpen(!navbarOpen)}
>
<FontAwesomeIcon icon={faBars} />
</NavButton>
</InnerMost>
...
}
export default Navbar
奇怪的是它在开发模式下表现出色。但是当我运行gatsby build
或部署它时,它不起作用。现在,如果您认为问题出在 FontAwesome,则不是。我更换了它,再次运行构建,它仍然无法正常工作。我查看了我的浏览器开发工具,一切看起来都很好,直到我看到了事件监听器部分。在开发中,你能想到的所有事件监听器,例如click
, close
, load
,mouseover
等都在那里。但是,在生产中,只有load
存在。
谁能帮帮我,拜托!
解决方案
推荐阅读
- c# - EF-Core 如何使用 Include 停止嵌套对象
- python - 如何在flask / python中打印出表值循环
- javascript - 我有一个随机显示标记的 SVG 地图。我可以准确地放置它们以告诉 SVG 地图上的实际位置吗?
- python - 如何在两行上加入数据框和字典
- c# - 从状态向量中找到真正的异常
- swift - 加载从 WKWebView 中注入的 AJAX GET 检索的 PDF 二进制文件到 PDFKit
- gwt - 如何从 GWT 上的 php Web 服务获取有效响应
- ios - 使用 Firebase 云功能时如何在 iOS 中发送通知?
- php - 从 WooCommerce 电子邮件中隐藏付款说明已完成的订单通知
- angular - Angular PWA 动态更改清单