我刚开始接触Gatsby和它的graphQL查询系统来获取资源。我有一个工作组件Image
,它可以获取图像并显示它。我想要定制图像的名称,但我不知道如何做到这一点。
这是正常工作的组件:
const Image = () => (
<StaticQuery
query={graphql`
query {
// fetching the image gatsby-astronaut.png
placeholderImage: file(relativePath: { eq: "gatsby-astronaut.png" }) {
childImageSharp {
fluid(maxWidth: 300) {
...GatsbyImageSharpFluid
}
}
}
}
`}
render={data => <Img fluid={data.placeholderImage.childImageSharp.fluid} />}
/>
);
以下是我尝试创建可自定义图像的内容:
const Image = ({ imgName }: { imgName: string }) => (
<StaticQuery
query={graphql`
query {
// fetching the image imgName
placeholderImage: file(relativePath: { eq: "${imgName}.png" }) {
childImageSharp {
fluid(maxWidth: 300) {
...GatsbyImageSharpFluid
}
}
}
}
`}
render={data => <Img fluid={data.placeholderImage.childImageSharp.fluid} />}
/>
);
但是对于此查询,它会引发以下错误:
预计1个参数,但得到2个.ts(2554)
我该如何拥有可自定义的图像名称?