【发布时间】:2021-01-03 21:37:31
【问题描述】:
请原谅这个新手。我想根据它们的类别列出一些文章,现在我有类别页面,您可以在其中单击一个类别,该特定类别下的文章列表应该打开。问题是,在哪里定义 slug 的变量 [等于文章的类别]。 鉴于我来自类别页面,在哪里定义等于类别的 $slug 变量,这必须是发送点击类别的发布请求,我应该把它放在那个页面的某个地方,如果我做了任何人可以指导我感觉这里?! 提前致谢。
const EduCatTemp = ({data}) => {
const {allStrapiEducations:{nodes:educations}} = data
return (
<Layout>
{
educations.map((education)=> {
return (
<p>{education.title}</p>
)
})
}
</Layout>
)
}
export default EduCatTemp
export const query = graphql`
{
allStrapiEducations(filter: {education_category: {slug: {eq: $slug}}}) {
nodes {
title
}
}
}
这是我的 gatsby-node.js 文件
educations: allStrapiEducations {
nodes {
slug
}
}
education_categories: allStrapiEducationCategories {
nodes {
slug
}
}
result.data.educations.nodes.forEach(education => {
createPage({
path: `/education/${education.slug}`,
component: path.resolve(`src/templates/education-template.js`),
context: {
slug: education.slug,
},
})
})
result.data.education_categories.nodes.forEach(educat => {
createPage({
path: `/education/${educat.slug}`,
component: path.resolve(`src/templates/education-category-template.js`),
context: {
slug: educat.slug,
},
})
})
这是我想从中获取信息的[父母]教育页面
import React from 'react'
import Layout from '../components/layout'
import EducationCard from '../components/EducationComponent/EducationCard'
import Category from '../components/utilities/Category'
const Education = ({data}) => {
const {allStrapiEducationCategories:{nodes:educations}} = data
return (
<Layout>
<div className="p-5">
<h1 className="sm:text-3xl text-2xl font-medium title-font text-gray-900 headline py-10">Beekeeping Programs</h1>
<input type="search" id="gsearch" placeholder="Search..." name="gsearch" className="my-5 py-2 search-button lg:w-1/3" />
<div className="flex flex-wrap mx-auto">
{educations.map((education)=> {
return <EducationCard headline={education.name} image={education.picture.childImageSharp.fixed} slug={`/education/${education.slug}`} />
})}
</div>
</div>
</Layout>
)
}
export default Education
export const query = graphql`
{
allStrapiEducationCategories {
nodes {
slug
picture {
childImageSharp {
fixed(width: 400
height: 200) {
...GatsbyImageSharpFixed
}
}
}
name
}
}
}
`
【问题讨论】:
-
这能回答你的问题吗? Variables in graphQL queries
-
我已经添加了 gatsby-node.js 页面,不幸的是这不是答案。顺便说一句,我用的是 Gatsby 和 Strapi。
标签: reactjs graphql gatsby graphiql