错误:“MyDocument.getInitialProps()”应该解析为带有“html”属性并设置了有效 html 字符串的对象

Tal*_*ota 6 typescript reactjs next.js

我的项目位于https://github.com/Talita1996/NLW4

  1. 我用命令创建了一个项目yarn create next-app project_name
  2. 我更改了一些文件的扩展名并添加了一些代码
  3. 我用命令安装了打字稿yarn add typescript @types/react @types/react-dom @types/node -D

第一天一切正常,但今天,当我打电话时,yarn dev我收到错误:

在此输入图像描述

小智 6

在您的 page/_document 中,您需要扩展 Document 类

import Document, { Html, Head, Main, NextScript } from 'next/document';

export default class MyDocument extends Document {

  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx)
    return { ...initialProps }
  }
  render() {
    return (
      <Html>
        <Head>
          <link rel="preconnect" href="https://fonts.gstatic.com" />
          <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Rajdhani:wght@600&display=swap" rel="stylesheet" />
        </Head>
        <body>
        <Main/>
        <NextScript/>
        </body>
      </Html>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 您能为 _document.js 的功能组件建议同样的事情吗?@你好地球 (10认同)

归档时间:

查看次数:

6708 次

最近记录:

2 年,11 月 前