首页 > 解决方案 > React.js 应用程序上的 Google 自动广告?

问题描述

我有一个在 React.js 上运行的网站前端。

我在 index.html 的 head 标记中添加了以下行。

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

以下代码向我显示错误,“'adsbygoogle' is not defined no-undef”

componentDidMount () {
        (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-9142841210062390",
              enable_page_level_ads: true
         });
}

如何让广告显示在我的 React.js Web 应用程序上?

这适用于 Google 自动广告,而不是广告单元。

标签: reactjsadsense

解决方案


您必须在 reactjs 应用程序中编写“window.adsbygoogle”而不是“adsbygoogle”

componentDidMount () {
    (window.adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-9142841210062390",
          enable_page_level_ads: true
     });
}

推荐阅读