尝试在我的 graphql 查询中为长而复杂的 Id 使用别名:
编译失败:graphql 标记中不允许字符串插值:
const query = graphql`
query MyQuery {
wordpress {
menu(id: "${wordpress("mainMenu")}") {
...rest of query
}
}
}
`
尝试在我的 graphql 查询中为长而复杂的 Id 使用别名:
编译失败:graphql 标记中不允许字符串插值:
const query = graphql`
query MyQuery {
wordpress {
menu(id: "${wordpress("mainMenu")}") {
...rest of query
}
}
}
`
您应该改用查询变量。
变量可以通过作为
createPageAPI 参数的上下文对象添加到页面查询(但不是静态查询)。文档
// inside template file
export const query = graphql`
query MyQuery($id: String!) {
menu(id: { eq: $id }) {
...rest of query
}
}
`
然后,您可以将此类变量作为页面上下文的一部分提供给gatsby-node.js. 例如:
// gatsby-node.js
const postTemplate = path.resolve(`./src/templates/post.js`)
allWordpressPost.edges.forEach(edge => {
createPage({
path: `/${edge.node.slug}/`,
component: slash(postTemplate),
context: {
id: edge.node.id, //
},
})
})
看看这个来自 gatsby的using-wordpress 示例。