【问题标题】:Adding meta tags on React.js在 React.js 上添加元标记
【发布时间】:2020-08-20 04:23:24
【问题描述】:

我正在尝试在每个页面上添加动态元标记。但仍然无法在我的页面源代码上看到我的元标记。我需要在 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>

【问题讨论】:

    标签: javascript reactjs meta-tags


    【解决方案1】:

    您应该为此使用React-helmet,因为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>
        );
      }
    };
    

    【讨论】:

    • 是的。我也尝试过使用 React-helmet 但同样 - 在源代码上看不到元标记。
    • 您是否尝试过在不使用 Fragment 的情况下使用头盔?
    • export default class Home extends Component { render() { return ( Home example.com" />
    • 仍然是一个片段。您能否编辑您的问题以将文件内容包含在 所在的位置?
    • 你的 public/index.html 中有什么?
    猜你喜欢
    • 2019-07-30
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2011-10-27
    • 2013-05-29
    • 2019-08-30
    • 2014-05-30
    相关资源
    最近更新 更多