Skip to content
← Back to rules

nextjs/no-head-element 正确性

An auto-fix is available for this rule.

它做了什么

防止在 Next.js 应用程序中使用原生的 <head> 元素。

为什么这是不好的?

<head> 元素可能导致 Next.js 应用程序出现意外行为。
请改用 Next.js 内置的 next/head 组件。

示例

此规则的 错误 代码示例:

jsx
function Index() {
  return (
    <>
      <head>
        <title>我的页面标题</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
      </head>
    </>
  );
}

export default Index;

此规则的 正确 代码示例:

jsx
import Head from "next/head";

function Index() {
  return (
    <>
      <Head>
        <title>我的页面标题</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
      </Head>
    </>
  );
}

export default Index;

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "plugins": ["nextjs"],
  "rules": {
    "nextjs/no-head-element": "error"
  }
}
bash
oxlint --deny nextjs/no-head-element --nextjs-plugin

参考资料