简体   繁体   English

如何在 React+Typescript 中使用微数据?

[英]How to use Microdata in React+Typescript?

When I add itemscope itemtype="http://schema.org/Product" to h1 , I get this error:当我将itemscope itemtype="http://schema.org/Product"添加到h1 ,出现此错误:

Type '{ children: string;输入'{孩子:字符串; itemscope: true;项目范围:真; itemtype: string;项目类型:字符串; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'. }' 不能分配给类型 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'。 Property 'itemscope' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'类型“DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>”上不存在属性“itemscope”

<h1 itemscope itemtype="http://schema.org/Product">Amasia</h1>

How to use Microdata in React+Typescript?如何在 React+Typescript 中使用微数据?

Not very elegant, but this works:不是很优雅,但这有效:

// foo.d.ts
declare namespace React {
    interface HTMLAttributes<T> {
        itemscope?: boolean;
        itemtype?: string;
    }
}
// app.tsx
function foo(): JSX.Element {
    return (
        <h1 itemscope itemtype="http://schema.org/Product">
            Amasia
        </h1>
    );
}

Works for me (Typescript 3.4.5).对我有用(Typescript 3.4.5)。

For React, it is case Sensitive.对于 React,它区分大小写。 Pay attention to: itemScope and itemType spelling注意: itemScopeitemType拼写

<div itemScope itemType={"http://schema.org/Product"}>{...}</div>

如果你使用打字稿,你应该这样写

<h1 itemScope itemType={"http://schema.org/Product"}>{...}</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM