代码之家  ›  专栏  ›  技术社区  ›  Maihan Nijat

如何在SPA中使用Google-DFA(Google-Publisher-Tag)?

  •  0
  • Maihan Nijat  · 技术社区  · 7 年前

    第一次广告显示良好,但第二次它给出空错误。

    OnInit 方法:

    addGoogleAd() {
      const googletag: any = window['googletag'] || {};
    
      if (window['googletag'] && googletag.apiReady) {
        googletag.cmd = googletag.cmd || [];
    
        googletag.cmd.push(() => {
          googletag.pubads().setTargeting('site', 'weatherinnovations');
          googletag.pubads().enableSingleRequest();
    
          googletag.defineSlot('/xx/weather_innovations', [[160, 600]], 'div-gpt-ad-xx-1')
            .setTargeting('position', ['s1']).addService(googletag.pubads());
          googletag.defineSlot('/xx/weather_innovations', [[300, 250]], 'div-gpt-ad-xx-2')
            .setTargeting('position', ['s2']).addService(googletag.pubads());
          googletag.defineSlot('/xx/weather_innovations', [[300, 250]], 'div-gpt-ad-xx-3')
            .setTargeting('position', ['s3']).addService(googletag.pubads());
          googletag.defineSlot('/76643716/weather_innovations', [[728, 90]], 'div-gpt-ad-xx-4')
            .setTargeting('position', ['s4']).addService(googletag.pubads());
          googletag.defineSlot('/xx/weather_innovations', [[728, 90]], 'div-gpt-ad-xx-5')
            .setTargeting('position', ['s5']).addService(googletag.pubads());
    
          googletag.pubads().collapseEmptyDivs(true, true);
          googletag.enableServices();
        });
        googletag.display(this.id);
      }
    }
    

    仪表板加载并显示ad,但当我从另一个组件返回时,它会给出以下错误:

    读取属性“setTargeting”为null

    我安慰孩子们 googletag 并且不是空的,并且具有相同的内容。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Maihan Nijat    7 年前

    错误不是描述性的,无法快速调试。现在,我正在销毁以前创建的插槽,然后再创建更多的,这样就解决了问题。用前面的线 googletag.cmd.push

    googletag.destroySlots();