【发布时间】:2022-01-27 01:48:46
【问题描述】:
我正在做一个 Gatsby 项目。
我想保留一个员工文件夹,每个员工都有自己的文件夹,例如 src/staff/hanna-rosenfeld/...,其中包含一个 index.mdx 和一个图像文件。
我想获取员工的姓名和图像以在组件中使用。
我的 gatsby 配置:
module.exports = {
siteMetadata: {
title: "Musikschule Weimar",
},
plugins: [
"gatsby-plugin-image",
"gatsby-plugin-sharp",
{
resolve: `gatsby-source-filesystem`,
options: {
name: `pages`,
path: `${__dirname}/src/pages/`,
},
},
{
resolve: "gatsby-source-filesystem",
options: {
name: `staff`,
path: `${__dirname}/src/staff`,
}
},
"gatsby-plugin-mdx",
"gatsby-transformer-sharp",
"gatsby-transformer-remark",
`gatsby-remark-images`,
],
};
我已经得到了执行下拉菜单的组件:
import * as React from 'react'
import { useState, useEffect } from "react"
import { useStaticQuery, graphql } from 'gatsby'
import { BiChevronDown } from "react-icons/bi";
import StaffList from "./StaffList"
const rows = [
{
id: 1,
title: "Verantwortliche",
},
{
id: 2,
title: "Lehrende der Zupfinstrumente",
instrument: "zupfinstrumente"
},
{
id: 3,
title: "Lehrende der Blechblasinstrumente",
},
{
id: 4,
title: "Lehrende des Tasteninstruments",
},
{
id: 5,
title: "Lehrende des Gesangs",
},
{
id: 6,
title: "Lehrende des Schlagzeugs",
},
{
id: 7,
title: "Lehrende des Akkordeons",
},
{
id: 8,
title: "Lehrende der Musiktheorie",
},
{
id: 9,
title: "Lehrende der Früherziehung",
}
]
class DropDownRows extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
return (
<div className="dropdown-rows">
{rows.map(row => (
<div key={row.id}>
<div className="row">
<div className="col">{row.title}</div>
<div className="col">
<BiChevronDown
onClick={this.handleClick}
style={{float: "right"}}/>
</div>
<div>
</div>
</div>
{this.state.isToggleOn ? <StaffList /> : ''}
</div>
))}
</div>
)
}
}
export default DropDownRows
src/staff/hanna-rosenfeld/index.mdx
---
title: Hanna Rosenfeld
featuredImage: ./Foto_05.jpg
---
Hi, mein Name ist Hanna und ich bin ein full time web developerin.
我的 StaffList 组件:
import * as React from 'react'
import { StaticQuery, graphql } from 'gatsby'
import { GatsbyImage, getImage } from "gatsby-plugin-image"
function StaffList({ data }) {
return(
<StaticQuery
query={graphql`
query staffQuery {
allMdx {
edges {
node {
id
body
frontmatter {
title
featuredImage {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
}
}
}
}
`}
render={data => (
<div>
<h1>{data.allMdx.edges.map(edge => <h1 key={edge.node.id} data={edge.node}>{edge.node.frontmatter.title}</h1>)}</h1>
<GatsbyImage alt='some alt text' image={getImage(data.allMdx.edges.map(edge => edge.node.frontmatter.featuredImage))} />
</div>
)}
/>
)
}
export default StaffList
查询 featuredImage 在 graphiql 中有效,但我无法显示图像。
控制台输出:
"警告:失败的道具类型:道具图像在 GatsbyImage,但它的值是未定义的。”
让名称仅显示在其类别中是另一个问题,现在我只想显示图像。
感谢您提前对可能的解决方案提供任何见解。
【问题讨论】: