在 React.js 上添加元标记

Adding meta tags on React.js

我正在尝试在每个页面上添加动态元标记。但是在我的页面源代码中仍然看不到我的元标记。我需要在 public/index.html 中添加一些东西吗...我没有使用服务器 我正在尝试在每个页面上添加动态元标记。但是在我的页面源代码中仍然看不到我的元标记。我需要在 public/index.html 中添加一些东西吗...我没有使用服务器 我正在尝试在每个页面上添加动态元标记。但是在我的页面源代码中仍然看不到我的元标记。我需要在 public/index.html 中添加一些东西吗...我没有使用服务器 例子: 首页

import {Helmet} from 'react-helmet';


export default class Home extends Component {
    render() {

        return (     
            <div> 
                <head>
                    <Helmet>
                        <meta charSet="utf-8" />
                        <title>Home</title>
                        <link rel="canonical" href="https://example.com/" />
                        <meta name="description" content="Some description here" />
                    </Helmet>  
            </head> 

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
    <link rel="manifest" href="/site.webmanifest">
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
    <meta name="msapplication-TileColor" content="#da532c">
    <meta name="theme-color" content="#ffffff">

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>Website</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

你应该使用 React-helmet for this as react-meta-tags 已经过时了。

如文档示例所示:

import React from "react";
import {Helmet} from "react-helmet";

class Application extends React.Component {
  render () {
    return (
        <div className="application">
            <Helmet>
                <meta charSet="utf-8" />
                <title>My Title</title>
                <link rel="canonical" href="http://example.com/example" />
            </Helmet>
            ...
        </div>
    );
  }
};