Reactjs 不更新元标题标记
我一直在通过以下操作设置react.js SPA的页面标题:Reactjs 不更新元标题标记,reactjs,single-page-application,meta-tags,react-helmet,Reactjs,Single Page Application,Meta Tags,React Helmet,我一直在通过以下操作设置react.js SPA的页面标题: document.title = {some title} …到目前为止,它运行良好。现在,我还想更新metatitle标签,所以我安装了。 在我的组件中,我从库中导入了头盔,并执行了以下操作: render() { {/* ... */} return ( <div> <Helmet> <title>{docTitle}</title>
document.title = {some title}
…到目前为止,它运行良好。现在,我还想更新meta
title
标签,所以我安装了。在我的组件中,我从库中导入了
头盔
,并执行了以下操作:
render() {
{/* ... */}
return (
<div>
<Helmet>
<title>{docTitle}</title>
<meta name="title" content={docTitle} />
</Helmet>
{/* ... */}
</div>
)
}
render(){
{/* ... */}
返回(
{docTitle}
{/* ... */}
)
}
打开页面时,我看到文档标题已正确更新,但在检查浏览器中的元素时,我注意到在我的
代码中,我在index.html
文件中添加了以下标记:
<meta name="title" content="Default Title">
使用react头盔更新该元标记时,在检查浏览器中的元素时,我仍然在index.html文件中看到该标记未更新。
然而,我没有注意到的是,在
的最底部,头盔显然添加了以下标签:
<meta name="title" content="Default Title" data-react-helmet="true">
我猜测(并希望)这将被搜索引擎机器人抓取
更新:
过了一段时间后,我可以验证Facebook和Google是否正确读取了头盔的标题标签。自述文件中没有记录该标签,但您必须将data react helmet=“true”
添加到要替换为react helmet的元标签中。例如,在您的index.html
中,元标记应该更新为如下所示:
<meta name="title" content="Default Title" data-react-helmet="true">
这应该在自述文件中,我不知道为什么不是,我看到一条评论说有人已经打开了一个PR来添加它