add raw HTML with [removed] inside Gatsby React page

前端 未结 5 1375
小蘑菇
小蘑菇 2020-12-01 18:13

I am trying to add an external embed code to my Gatsby page.

I currently use

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


let test          


        
5条回答
  •  粉色の甜心
    2020-12-01 18:37

    You can inject external scripts (with no npm modules) to gatsby.js project in many ways. Prefer to use respective gatsby-plugin for "web-analytics" scripts.

    Using require() :

    • Create a file in your project myScript.js and paste the script content
    • Add const myExtScript = require('../pathToMyScript/myScript')
      to a statefull component at the Pages folder inside render() and before return() if you want to execute that script only at that page(=page/component scope).

      export default class Contact extends React.Component {  
        render() {  
         const myExtScript = require('../pathToMyScript/myScript')  
          return (
            ........       
                     )}
      
    • If you want to execute script in the global scope (=in every page/ component):
      add it in html.js

      
      
                                       
                    
提交回复
热议问题