刷新Angular路线上的Google广告(Adsense)
我无法在布局组件上的路线更改上刷新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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用rxjs
acrapiorSubject
>在banner Service中创建 experioSubject
caping> caping
//创建的
bannershow
可观察到async
管道temporarly solved with rxjs
BehaviorSubject
// created
BehaviorSubject
in banner service// emitted new value on
NavigationEnd
Observable of router within app component// subscribed to
bannerShow
Observable withasync
pipe