刷新Angular路线上的Google广告(Adsense)

发布于 2025-01-21 07:04:34 字数 1370 浏览 0 评论 0原文

我无法在布局组件上的路线更改上刷新Google Adsense 这是我的应用程序路线修饰

  //app-routing-module
  {
    path: '',
    component: LayoutComponent,
    children: [
      {
        path: '',
        loadChildren: () => import('./pages/pages.module').then(m => m.PagesModule)
      }
    ]
  },

是路由模块

const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
  },

Google Ad代码在单独的横幅组件

  //banner component ts


  const navEndEvents = this.router.events.pipe(filter(event => event instanceof NavigationEnd))
    navEndEvents.subscribe((event: any) => {
      this.initGoogleAd();
    });
  private initGoogleAd() {
    try {
      (adsbygoogle = window.adsbygoogle || []).push({});

    } catch (e) { }
  }

HTML中

  <div>
    <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6053771666988490"
                data-ad-slot="9396915897" data-ad-format="auto" data-full-width-responsive="true"></ins>
  </div>

横幅

组件

//..some other elements
  <app-banner></app-banner> //google ads not refreshing without reload
//..

这里

..some other elements
  <app-banner></app-banner> //works on home component though
..

i m unable to refresh the google adsense on route change on layout component
here is my app-routing modue

  //app-routing-module
  {
    path: '',
    component: LayoutComponent,
    children: [
      {
        path: '',
        loadChildren: () => import('./pages/pages.module').then(m => m.PagesModule)
      }
    ]
  },

here is pages-routing module

const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
  },

google ad code is in separate banner component

  //banner component ts


  const navEndEvents = this.router.events.pipe(filter(event => event instanceof NavigationEnd))
    navEndEvents.subscribe((event: any) => {
      this.initGoogleAd();
    });
  private initGoogleAd() {
    try {
      (adsbygoogle = window.adsbygoogle || []).push({});

    } catch (e) { }
  }

banner component html

  <div>
    <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6053771666988490"
                data-ad-slot="9396915897" data-ad-format="auto" data-full-width-responsive="true"></ins>
  </div>

Every thing works fine I m unable to rerender the google banner component on layout component

layout component

//..some other elements
  <app-banner></app-banner> //google ads not refreshing without reload
//..

Home component

..some other elements
  <app-banner></app-banner> //works on home component though
..

any help will be appreciated thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清旖 2025-01-28 07:04:34

用rxjs acrapiorSubject

>在banner Service中创建 experioSubject

export class BannerService extends ApiService {
  bannerShow  = new BehaviorSubject<boolean>(true)
}

caping> caping

navEndEvents.subscribe((event: any) => {
  this.bannerService.bannerShow.next(false)
  this.bannerService.all(3).subscribe((res: any) => {
    this.bannerService.bannerShow.next(true)
  });
});

//创建的 bannershow可观察到async管道

//layout component ts
<app-banner *ngIf="bannerService.bannerShow | async"></app-banner>

temporarly solved with rxjs BehaviorSubject

// created BehaviorSubject in banner service

export class BannerService extends ApiService {
  bannerShow  = new BehaviorSubject<boolean>(true)
}

// emitted new value on NavigationEnd Observable of router within app component

navEndEvents.subscribe((event: any) => {
  this.bannerService.bannerShow.next(false)
  this.bannerService.all(3).subscribe((res: any) => {
    this.bannerService.bannerShow.next(true)
  });
});

// subscribed to bannerShow Observable with async pipe

//layout component ts
<app-banner *ngIf="bannerService.bannerShow | async"></app-banner>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文