next.js 在jsx中包含含有大括号javascript代码[duplicate]

hm2xizp9  于 2022-11-23  发布在  Java
关注(0)|答案(1)|浏览(139)

此问题在此处已有答案

How to add tag using ReactJS?(4个答案)
3天前关闭.

return <Elements stripe={stripePromise}>
        <IntlProvider locale={locale} messages={messages} textComponent={Fragment}>
            <GoogleOAuthProvider clientId={GOOGLE_CLIENT_ID}>
                <div
                    onDragOver={handleDragOver}
                    onDrop={handleDrop}
                    onMouseOver={mouseOver}
                >
                    <Head>
                        <link rel='shortcut icon' href={favicon} type='image/png' />
                    </Head>
                    <div>
                        <div className={styles.page} onClick={handleCloseSupportChat}>
                            <div className={styles.pageContent}>
                                <Component {...pageProps} />
                            </div>
                        </div>
                    </div>
                    <script>
                        window.__lc = window.__lc || {};
                        window.__lc.license = 14728555;
                        ;(function(n,t,c){function i(n){return e._h?e._h.apply(null,n):e._q.push(n)}var e={_q:[],_h:null,_v:"2.0",on:function(){i(["on",c.call(arguments)])},once:function(){i(["once",c.call(arguments)])},off:function(){i(["off",c.call(arguments)])},get:function(){if(!e._h)throw new Error("[LiveChatWidget] You can't use getters before load.");return i(["get",c.call(arguments)])},call:function(){i(["call",c.call(arguments)])},init:function(){var n=t.createElement("script");n.async=!0,n.type="text/javascript",n.src="https://cdn.livechatinc.com/tracking.js",t.head.appendChild(n)}};!n.__lc.asyncInit&&e.init(),n.LiveChatWidget=n.LiveChatWidget||e}(window,document,[].slice))
                    </script>
                    <noscript><a href="https://www.livechat.com/chat-with/14728555/" rel="nofollow">Chat with us</a>, powered by <a href="https://www.livechat.com/?welcome" rel="noopener nofollow" target="_blank">LiveChat</a></noscript>
                </div>
            </GoogleOAuthProvider>
        </IntlProvider>
    </Elements>;
};

上图是jsx代码,代码内

<script>
                    window.__lc = window.__lc || {};
                    window.__lc.license = 14728555;
                    ;(function(n,t,c){function i(n){return e._h?e._h.apply(null,n):e._q.push(n)}var e={_q:[],_h:null,_v:"2.0",on:function(){i(["on",c.call(arguments)])},once:function(){i(["once",c.call(arguments)])},off:function(){i(["off",c.call(arguments)])},get:function(){if(!e._h)throw new Error("[LiveChatWidget] You can't use getters before load.");return i(["get",c.call(arguments)])},call:function(){i(["call",c.call(arguments)])},init:function(){var n=t.createElement("script");n.async=!0,n.type="text/javascript",n.src="https://cdn.livechatinc.com/tracking.js",t.head.appendChild(n)}};!n.__lc.asyncInit&&e.init(),n.LiveChatWidget=n.LiveChatWidget||e}(window,document,[].slice))
                </script>
                <noscript><a href="https://www.livechat.com/chat-with/14728555/" rel="nofollow">Chat with us</a>, powered by <a href="https://www.livechat.com/?welcome" rel="noopener nofollow" target="_blank">LiveChat</a></noscript>

属于https://www.livechat.com/,要求为他们的聊天机器人包括;他们安装指南:https://www.livechat.com/help/setting-up-livechat-on-your-website/?utm_source=webapp
但我有以下错误:

Syntax error: Unexpected token, expected "}"

  288 |                         window.__lc = window.__lc || {};
  289 |                         window.__lc.license = 14728555;
> 290 |                         ;(function(n,t,c){function i(n){return e._h?e._h.apply(null,n):e._q.push(n)}var e={_q:[],_h:null,_v:"2.0",on:function(){i(["on",c.call(arguments)])},once:function(){i(["once",c.call(arguments)])},off:fun
ction(){i(["off",c.call(arguments)])},get:function(){if(!e._h)throw new Error("[LiveChatWidget] You can't use getters before load.");return i(["get",c.call(arguments)])},call:function(){i(["call",c.call(arguments)])},init:function(){va
r n=t.createElement("script");n.async=!0,n.type="text/javascript",n.src="https://cdn.livechatinc.com/tracking.js",t.head.appendChild(n)}};!n.__lc.asyncInit&&e.init(),n.LiveChatWidget=n.LiveChatWidget||e}(window,document,[].slice))     
      |                                                                                                     ^
  291 |                     </script>
  292 |                     <noscript><a href="https://www.livechat.com/chat-with/14728555/" rel="nofollow">Chat with us</a>, powered by <a href="https://www.livechat.com/?welcome" rel="noopener nofollow" target="_blank">LiveChat</a></
noscript>
  293 |                 </div>

我应该如何解决它?

mrphzbgm

mrphzbgm1#

script标签内的代码只是普通的js,script标签用于html文件,并且您希望在其中包含一些js。由于您已经在使用js,因此您可以创建一个包含script标签内逻辑的函数,并在需要时从代码中调用它,这种情况可能是组件挂载时。
或者,您可以添加一个iframe并在其中包含script标记,您可以遵循此示例。

相关问题