类型错误:无法读取未定义的属性(读取“站点”)
您好,我遇到了这个错误,我尝试了很多方法,但不知道如何解决。 它可能是一个库还是我需要做其他事情?
这里有错误和我找到它的代码。
> error - components/Layout.tsx (31:4) @ Layout
> TypeError: Cannot read properties of undefined (reading 'site')
> 29 |
> 30 | const { name, url, title, description, socialPreview } =
> > 31 | publicRuntimeConfig.site;
> | ^
> 32 |
> 33 | const meta = {
> 34 | name,
Layout.tsx(它是一个组件,我在我正在开发的另一个网站中具有相同的结构,但我在该网站中没有错误)
import { setConfig } from 'next/config'
import { publicRuntimeConfig } from '../next.config';
import getConfig from 'next/config';
import Head from 'next/head';
import { useRouter } from 'next/router';
setConfig({ publicRuntimeConfig })
type LayoutProps = {
title?: string;
description?: string;
date?: string;
socialPreview?: string;
children: React.ReactNode;
};
const Layout = ({ children, ...customMeta }: LayoutProps) => {
const router = useRouter();
const { asPath } = router;
const { name, url, title, description, socialPreview } =
publicRuntimeConfig.site;
const meta = {
name,
url,
title,
description,
socialPreview,
...customMeta,
};
return (
<>
<Head>
<link rel="icon" href="/images/fev.svg" key="favicon" />
<link rel="canonical" href={`${url}${asPath}`} key="canonical" />
{/* Twitter */}
<meta
name="twitter:card"
content="summary_large_image"
key="twitter_card"
/>
<meta name="twitter:title" content={meta.title} key="twitter_title" />
<meta
name="twitter:description"
content={meta.description}
key="twitter_description"
/>
<meta
name="twitter:image"
content={`${url}${socialPreview}`}
key="twitter_image"
/>
{/* Open Graph */}
<meta property="og:url" content={`${url}${asPath}`} key="og_url" />
<meta property="og:site_name" content={meta.name} key="og_site_name" />
<meta property="og:title" content={meta.title} key="og_title" />
<meta
property="og:description"
content={meta.description}
key="og_description"
/>
<meta
property="og:image"
content={`${url}${socialPreview}`}
key="og_image"
/>
<meta property="og:image:width" content={`1200`} key="og_image_width" />
<meta
property="og:image:height"
content={`630`}
key="og_image_height"
/>
<meta name="description" content={meta.description} key="description" />
{meta.date && (
<meta property="article:published_time" content={meta.date} />
)}
<title key="title">{meta.title}</title>
</Head>
<main>{children}</main>
</>
);
};
export default Layout;
Hello, I got this error, I tried a lot of things but I don't know how to solve it.
Could it be a library or do I need to do something else?
Here you have the error and the code where I found it.
> error - components/Layout.tsx (31:4) @ Layout
> TypeError: Cannot read properties of undefined (reading 'site')
> 29 |
> 30 | const { name, url, title, description, socialPreview } =
> > 31 | publicRuntimeConfig.site;
> | ^
> 32 |
> 33 | const meta = {
> 34 | name,
Layout.tsx (It is a component, I have the same structure in another website I'm working on, but I have no errors in that one)
import { setConfig } from 'next/config'
import { publicRuntimeConfig } from '../next.config';
import getConfig from 'next/config';
import Head from 'next/head';
import { useRouter } from 'next/router';
setConfig({ publicRuntimeConfig })
type LayoutProps = {
title?: string;
description?: string;
date?: string;
socialPreview?: string;
children: React.ReactNode;
};
const Layout = ({ children, ...customMeta }: LayoutProps) => {
const router = useRouter();
const { asPath } = router;
const { name, url, title, description, socialPreview } =
publicRuntimeConfig.site;
const meta = {
name,
url,
title,
description,
socialPreview,
...customMeta,
};
return (
<>
<Head>
<link rel="icon" href="/images/fev.svg" key="favicon" />
<link rel="canonical" href={`${url}${asPath}`} key="canonical" />
{/* Twitter */}
<meta
name="twitter:card"
content="summary_large_image"
key="twitter_card"
/>
<meta name="twitter:title" content={meta.title} key="twitter_title" />
<meta
name="twitter:description"
content={meta.description}
key="twitter_description"
/>
<meta
name="twitter:image"
content={`${url}${socialPreview}`}
key="twitter_image"
/>
{/* Open Graph */}
<meta property="og:url" content={`${url}${asPath}`} key="og_url" />
<meta property="og:site_name" content={meta.name} key="og_site_name" />
<meta property="og:title" content={meta.title} key="og_title" />
<meta
property="og:description"
content={meta.description}
key="og_description"
/>
<meta
property="og:image"
content={`${url}${socialPreview}`}
key="og_image"
/>
<meta property="og:image:width" content={`1200`} key="og_image_width" />
<meta
property="og:image:height"
content={`630`}
key="og_image_height"
/>
<meta name="description" content={meta.description} key="description" />
{meta.date && (
<meta property="article:published_time" content={meta.date} />
)}
<title key="title">{meta.title}</title>
</Head>
<main>{children}</main>
</>
);
};
export default Layout;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论