如何在react js中动态包含元标记?

IT技术 facebook reactjs
2021-05-07 17:55:33

我正在使用 reactjs ,我想在 facebook 上分享我的内容,它正在发生,但分享后它没有显示图像、标题、内容描述。所以我使用react-helmet在 index.html 中动态添加元标记。

<Helmet>
<meta property="og:type" content="video.other" />
<meta property="og:image"       content="https://www.w3schools.com/css/trolltunga.jpg" />
<meta property="og:title" content="My Title" />
<meta property="og:url" content="https://www.afnity.com/video/155" />
<meta property="og:description" content="some discription of the shared    content" />

</Helmet>

这是分享按钮

   <button> <a title="dummy" 
href="http://www.facebook.com/sharer.php? u=https://dummy.com/videos/id=25"     target="_blank">
share</a>
</button>

但它不起作用。

4个回答

这可以在您的 React 应用程序中启用服务器端渲染时使用 react-document-meta npm module来实现

const meta = {
      title: 'Samvikshana - New Perspective of Exploration',
      meta: {
        property: {
          'og:title': 'Samvikshana',
          'og:url': 'https://samvikshana.weebly.com/',
          'og:image': imageUri,
          'og:description': 'New Perspective of Exploration',

          'twitter:card': 'summary_large_image',
          'twitter:title': 'Samvikshana',
          'twitter:description': 'New Perspective of Exploration',
          'twitter:image': imageUri
        }
      }
    };

    return (
      <div className='container-fluid'>
        <DocumentMeta {...meta} />
         </div>);
    }

阅读此博客了解更多信息 - https://samvikshana.weebly.com/blog/dynamic-meta-tags-in-react-components

标签需要在服务器端呈现。

这里的其他答案似乎专注于纯 JS 解决方案。

对于那些在 .Net MVC 应用程序上工作的人,我的解决方案描述如下:

网站的主要入口点是通过Index()默认的 Home Controller方法,如下所示:

//Home Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

无论您输入什么 URL,它都会首先出现在此处。

关键是使用来自 HttpRequest 参数的 URL,通过将其馈送到 View 中,如下所示:

// Home Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View(Request);
    }
}

首先,如果您的网站使用共享布局页面,请不要忘记在<head>标签内创建一个 RenderSection ,如下所示:

//Layout.cshtml

<head>
    @RenderSection("meta", required: false) 
</head>

现在在 中Index.cshtml,设置HttpRequest为视图模型,从请求路径中取出 URL 并<meta>根据需要将其放置在标签中(meta如果您制作了一个请将其全部放在RenderSection 中)。

完整路径值的格式为"/pathName/"

//Index.cshtml

@model Microsoft.AspNetCore.Http.HttpRequest

@section meta  {

    @if (Model.Path.Value.Contains("pathName"))
    {
        <meta property="og:url" content="https://mywebsite.com/@Model.Path.Value.Substring(1)" />
    }
}

也可以做很多其他的事情。例如,我的网站有基于数据库 ID 的请求路径,即/pathName/45,所以我的工作看起来有点像这样:

//Index.cshtml

@model Microsoft.AspNetCore.Http.HttpRequest

@section meta  {

    @if (Model.Path.Value.Contains("pathName"))
    {
        MyWebsite.Models.DatabseContext db = new MyWebsite.Models.DatabseContext();
        int itemID = int.Parse(Model.Path.Value.Substring(10, Model.Path.Value.Length - 10));
        Item item = db.Items.Find(itemID);

        <meta property="og:title" content="@item.Name" />
        <meta property="og:description" content="@item.Description" />
        <meta property="og:url" content="https://mywebsite.com/@Model.Path.Value.Substring(1)" />
        <meta property="og:image" content="@item.ImageURL" />
    }
}

有了这个,我可以为我的 React 网站的任何页面呈现非常详细和特定的元标记。到目前为止工作完美!

默认情况下,应用程序将生成以下标题

<title>React App</title>

更改非常简单,只需将此生命周期挂钩放入您的应用程序文件即可

componentDidMount() {
document.title = 'Custom Title';  }

用下面的代码替换头盔组件。

<Helmet
  meta={[
    {"property": "og:type", "content": "video.other"}
    {"property": "og:image", "content": "https://www.w3schools.com/css/trolltunga.jpg"}
    {"property": "og:title", "content": "My Title"}
    {"property": "og:url", "content": "https://www.afnity.com/video/155"}
    {"property": "og:description", "content": "some discription of the shared    content"}
  ]}
/>

这将根据您的要求添加元标记。