我有一个定义了 frontmatter 的 mdx gatsby 页面。
---
title: About Me
path: /about
description: Some information about me.
---
# About Me
[Twitter](https://twitter.com/RainFire336)
我在以下配置了默认布局gatsby-config.js
:
{
resolve: 'gatsby-plugin-mdx',
options: {
defaultLayouts: {
default: require.resolve("./src/components/page-layout.tsx")
},
gatsbyRemarkPlugins: [
'gatsby-remark-images',
{
resolve: 'gatsby-remark-prismjs',
options: {
showLineNumbers: true,
prompt: {
user: 'rain',
},
},
},
],
},
}
布局尝试使用它的 props 访问 frontmatter:
import React from 'react';
import { Layout } from './layout';
import { Card } from './card';
import { Metadata } from './metadata';
interface Props {
children: React.ReactNode;
uri: string;
pageContext: { frontmatter: any };
}
export default function (p: Props) {
const { children, uri, pageContext } = p;
return (
<Layout>
<Metadata {...pageContext.frontmatter} url={uri} />
<Card style={{ width: '50%' }}>{children}</Card>
</Layout>
);
}
问题是 frontmatter 总是一个空对象: 所以要形成一个问题。如何访问我的 frontmatter?