【问题标题】:Typescript Parsing error: '}' expected when trying to use Google Analytics打字稿解析错误:尝试使用 Google Analytics 时需要“}”
【发布时间】:2020-08-11 15:22:43
【问题描述】:

我正在尝试将 Google Analytics 添加到我的 React 应用程序中,并且我在 HTML 文件的头部有 Script 标签,但我希望有条件地渲染这部分脚本。如果我能提供帮助,我不想使用任何依赖项。下面的代码是我正在使用的,但我得到一个解析错误。任何人都可以阐明我的困境吗?

import React from 'react';

interface IGoogleAnalytics {
  arguments?: any
}

const GoogleAnalytics: React.FC<IGoogleAnalytics> = () => {
  return (
    <script>
      {
        window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};
        ga.l=+new Date;
        ga('create', 'UA-000000000-1', 'auto');
        ga('send', 'pageview');
      }
    </script>
  )
}

export default GoogleAnalytics

【问题讨论】:

标签: reactjs typescript google-analytics


【解决方案1】:

如果您不想使用任何依赖项,请将您的脚本标签移动到 Google Analytics 脚本文件之后的 HTML 文件中。

<script src="link to Google Analytics script" />
<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};
    ga.l=+new Date;
    ga('create', 'UA-000000000-1', 'auto');
    ga('send', 'pageview');
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2023-04-01
    • 2020-09-17
    • 2023-02-02
    • 2021-03-09
    • 2021-02-05
    • 2015-07-31
    相关资源
    最近更新 更多