繁体   English   中英

React js index.html react道具

[英]React js index.html react props

错误:找不到反应道具

我如何在index.html page.main页面中访问我想呈现一些meta标签属性的react props。 元标记来自后端。

我想在index.html page.index.html中获取商​​店数据不是组件,所以我该怎么做。

很难提供帮助,因为您一开始没有提供任何代码。 希望接下来会有所帮助。

首先通过运行npm i react-meta-tag --save安装react-meta-tags 然后创建一个组件,如下所示:来自react-meta-tags的代码

import React from 'react';
import MetaTags from 'react-meta-tags';

class Metas extends Component {
    render(){
        return (
          <div>
              <MetaTags>
                  <title>{props.title}</title>
                  <meta name="description" content={props.description} />
                  <meta property="og:title" content={props.og-title} />
                  <meta property="og:image" content={props.og-image} />
              </MetaTags>
          </div>
      )
    }
}

然后,您可以在另一个组件中使用此组件,如下所示:

import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import Metas from './Metas'

class UseMeta extends Component {
    render(){
        return(
            <Metas 
                title="Awesome website"
                description="Awesome description about my website"
                og-title="Another description"
                og-image="./public/img/awesome-image.png"
            />
        )
    }
}

ReactDOM.render(,document.getElementById('root'))只要您的html中有id="root"UseMeta就会自动添加到html中

<!doctype html>
<html lang="en">
  <head>
  </head>
  <body>
    <div id="root"></div>
    <script src="./UseMeta.js"></script>
  </body>
</html>

您可能需要使用babel。 要获得完整的教程,请在不到1小时的时间内使用Npm,Babel 6和Webpack来查看Setup React.js。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM