nextjs 触发器中的头脚本应用程序/ld+json ESLint:无法读取未定义的属性'0'



我有一些类似的代码。我需要添加application/ld+json结构化数据,但这样做会触发这个ESLint错误:

CCD_ 2,它指的是CCD_。即使我只在变量没有未定义的情况下生成代码,我也会收到这个错误。如何修复?

import Head from 'next/head';
import { IHeadTags } from '../../../utils/types';
export const Index: React.FC<IHeadTags> = (props: IHeadTags) => {
scriptStringProduct = { __html: '' },
isProduct = false
} = props;
return (
<Head>
{isProduct && scriptStringProduct !== undefined && (
<script
type='application/ld+json'
dangerouslySetInnerHTML={scriptStringProduct ?? { __html: '' }}
key='scriptStringProduct-jsonld'
/>
)}
</Head>
);
};
export default Index;

.eslintrc.json文件中,您需要关闭next-script-for-ga的规则。

示例:

// .eslintrc.json
{
"settings": {
"react": {
"version": "detect"
}
},
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"next",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint"],
"rules": {
"react/prop-types": "off",
"react/react-in-jsx-scope": "off",
"@typescript-eslint/no-explicit-any": "off",
"@next/next/no-document-import-in-page": "off",
"@next/next/next-script-for-ga": "off" // make sure to have this rule off
}
}

相关内容

  • 没有找到相关文章

最新更新