繁体   English   中英

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

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

当我将itemscope itemtype="http://schema.org/Product"添加到h1 ,出现此错误:

输入'{孩子:字符串; 项目范围:真; 项目类型:字符串; }' 不能分配给类型 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'。 类型“DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>”上不存在属性“itemscope”

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

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

不是很优雅,但这有效:

// 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>
    );
}

对我有用(Typescript 3.4.5)。

对于 React,它区分大小写。 注意: 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