簡體   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