在Gatsby React页面中使用<script>添加原始HTML

Gia*_*las 12 javascript reactjs gatsby

我正在尝试将外部嵌入代码添加到我的Gatsby页面.

我目前正在使用

import React from 'react'
import Link from 'gatsby-link'


let test ="<script type='text/javascript'>
(function(d, s) {
    var useSSL = 'https:' == document.location.protocol;
    var js, where = d.getElementsByTagName(s)[0],
    js = d.createElement(s);
    js.src = (useSSL ? 'https:' : 'http:') +  '//www.peopleperhour.com/hire/1002307300/1213788.js?width=300&height=135&orientation=vertical&theme=light&rnd='+parseInt(Math.random()*10000, 10);
    try { where.parentNode.insertBefore(js, where); } catch (e) { if (typeof console !== 'undefined' && console.log && e.stack) { console.log(e.stack); } }
}(document, 'script'));
</script>"

const ContactPage = () => (

    <div>
        <h1>ContactPage</h1>
        <div
            dangerouslySetInnerHTML={{ __html: test }}
          />
    </div>

)

export default ContactPage
Run Code Online (Sandbox Code Playgroud)

这充满了语法错误.你能指出一个更好的方法来在反应组件中包含原始片段吗?

在Gatsby中是否还有一个地方可以添加所有其他脚本,如自定义脚本,Google Analytics,图标字体,animate.js以及我可能需要的任何其他内容?

感谢您的帮助

And*_*asT 16

您可以通过多种方式将外部脚本(没有npm模块)注入gatsby.js项目.更喜欢将"gatsby-plugin"用于"web-analytics"脚本.

使用require():

  • 在项目中创建一个文件myScript.js并粘贴脚本内容
  • 如果要仅在该页面(=页面/组件范围)执行该脚本,请在Pages文件夹内部和之前添加const myExtScript = require('../pathToMyScript/myScript')
    statefull组件. render()return()

    export default class Contact extends React.Component {  
      render() {  
       const myExtScript = require('../pathToMyScript/myScript')  
        return (
          ........       
                   )}
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果要在全局范围内执行脚本(=在每个页面/组件中):
    将其添加进去html.js

    <script dangerouslySetInnerHTML= {{ __html: ` 
        putYourSciptHereInBackticks `}} />`  
    
    Run Code Online (Sandbox Code Playgroud)

    关闭之前</body>.最好在这个组件中操作/监视您的全局范围,因为它具有这个特定目的......将html注入全局的每个页面/路由.

  • 全局范围注入的另一种方法是require()在组件声明之前.这可行,但这不是一个好的做法,因为你的组件不应该全局注入任何东西.

     const myExtScript = require('../pathToMyScript/myScript')  
    
       export default class Contact extends React.Component {  
        render() {  
          return (
          ........       
                   )}
    
    Run Code Online (Sandbox Code Playgroud)

PS对不起,如果答案没有正确编辑.这是我在StackOverflow上的第一个答案.


Moh*_*ria 8

使用React头盔

第一 import Helmet from 'react-helmet'

在你里面div你可以这样做

<Helmet>
<script type='text/javascript'>
(function(d, s) {
    var useSSL = 'https:' == document.location.protocol;
    var js, where = d.getElementsByTagName(s)[0],
    js = d.createElement(s);
    js.src = (useSSL ? 'https:' : 'http:') +  '//www.peopleperhour.com/hire/1002307300/1213788.js?width=300&height=135&orientation=vertical&theme=light&rnd='+parseInt(Math.random()*10000, 10);
    try { where.parentNode.insertBefore(js, where); } catch (e) { if (typeof console !== 'undefined' && console.log && e.stack) { console.log(e.stack); } }
}(document, 'script'));
</script>
</Helmet>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但这会产生与我的初始代码相同的语法错误。无法转译 (3认同)