【问题标题】:How to implement zendesk web widget to specific page on gatsby如何在 gatsby 上的特定页面上实现 zendesk Web 小部件
【发布时间】:2020-10-29 06:36:09
【问题描述】:

将 zendesk 安装脚本添加到 html.js 文件正在按预期工作,并且小部件显示在所有页面上。

我想在特定页面上显示它,例如 about-us 页面。 这个我试过了

render() {
return (
<>
<script
    id="ze-snippet"
    src="https://static.zdassets.com/ekr/snippet.js?key=xxx-xxxxx"
>
</script>
...
</>

但它不起作用。 有什么想法吗?

【问题讨论】:

    标签: reactjs gatsby zendesk


    【解决方案1】:

    在 index.html 文件中

    <html>
        <body>
            <!-- your scripts -->
    
            <script id="ze-snippet" src="https://static.zdassets.com/ekr/snippet.js?key={YOUR_KEY}"> </script>
            <!-- Hide de widget by default -->
            <script>zE('webWidget', 'hide')</script>
        </body>
    </html>
    

    在你的组件中展示

    import React from 'react'
    
    class MyComponent extends React.Component {
    
        constructor (props) {
            super(props)
        }
    
        componentDidMount () {
            // show widget on mount component
            zE('webWidget', 'show')
        }
    
        componentDidUnmount () {
            // hide widget on unmount component
            zE('webWidget', 'hide')
        }
    
        render () {
            // your render
            return (
                <div>Hello World</div>
            )
        }
    }
    

    在其他组件中不需要做任何事情

    【讨论】:

      【解决方案2】:

      要使用 React(以及使用 Gatsby)在您的页面中添加 &lt;scripts&gt;,您有多种选择。最容易实现的是使用&lt;Helmet&gt; 组件。您可以在their docs 中查看更多信息。基本上,它会处理你的&lt;head&gt; 标签:

      这个可重用的 React 组件将管理你对 文件头。

      Helmet 采用纯 HTML 标签并输出纯 HTML 标签。它死了 简单且对 React 初学者友好。

      要仅在 /about-us 页面中显示此脚本,您需要在所需页面中插入此 sn-p:

      import React from "react"
      import Helmet from "react-helmet"
      import Layout from "../components/layout"
      import SEO from "../components/seo"
      
      const AboutUs= () => (
       <>
         <>
            <Helmet>
              <script id="ze-snippet" src="https://static.zdassets.com/ekr/snippet.js?key=xxx-xxxxx">
            </Helmet>
         </>
          <Layout>
            <SEO title="About us" />
           <h1>I'm the about page</h1>
          </Layout>
       </>  
       )
      
      export default AboutUs
      

      不管你是有状态的还是无状态的组件,想法是在&lt;Helmet&gt; 组件之间包装任何你想放在&lt;head&gt; 中的东西,在这种情况下是&lt;script&gt;。

      【讨论】:

      • 感谢您的回答。这样,它在about-us 页面上运行良好。但是,当您转到着陆页或其他页面时,小部件仍然存在,这是意料之外的。
      • 我猜这是因为它在页面之间共享的 组件内部。我用另一个猜测更新了我的答案。告诉我它是如何工作的!
      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多