gpt4 book ai didi

node.js - 以下 Gatsby 教程卡在 "unexpected use of location"上

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:22 33 4
gpt4 key购买 nike

我在 Gatsby 应用程序中收到此错误。我的代码似乎与我正在遵循的教程相同,我似乎无法弄清楚出了什么问题。错误是

50:10 error Unexpected use of 'location' no-restricted-globals

还有其他页面,可能导入的某些内容导致了冲突。我使用的是 Node 版本 v11.0.0

代码如下:

src/components/layout.js

import React from 'react'
import PropTypes from 'prop-types'
import Img from 'gatsby-image'
import Helmet from 'react-helmet'
import styled from 'styled-components'
import { StaticQuery, graphql } from 'gatsby'

import Header from './header'
import Archive from './archive'
import './layout.css'

const MainLayout = styled.main`
max-width: 90%;
margin: 0 auto;
display: grid;
grid-template-columns: 6fr 1fr;
grid-gap: 40px;
`

const Layout = ({ children }) => (
<StaticQuery
query={graphql`
query SiteTitleQuery {
site {
siteMetadata {
title
}
}
file(relativePath: { regex: "/bg/" }) {
childImageSharp {
fluid(maxWidth: 1000) {
...GatsbyImageSharpFluid
}
}
}
}
`}
render={data => (
<>
<Helmet
title={data.site.siteMetadata.title}
meta={[
{ name: 'description', content: 'Sample' },
{ name: 'keywords', content: 'sample, something' },
]}
>
<html lang="en" />
</Helmet>
<Header siteTitle={data.site.siteMetadata.title} />
//**HERE IS WHERE THE ERROR IS HAPPENING**
{location.pathname === '/' && (
<Img fluid={data.file.childImageSharp.fluid} />
)}
<MainLayout>
<div>{children}</div>
<Archive />
</MainLayout>
</>
)}
/>
)

Layout.propTypes = {
children: PropTypes.node.isRequired,
}

export default Layout

最佳答案

您需要将 location 添加到组件声明中:

const Layout = ({ children, location }) => ( ... )

关于node.js - 以下 Gatsby 教程卡在 "unexpected use of location"上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53693840/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com