>我尝试在 Ionic 2 中使用此插件,如图所示,并添加在 src/app/app.components.ts 内部;
import { TabsPage } from '../pages/tabs/tabs';
import { AdMobFree, AdMobFreeBannerConfig } from '@ionic-native/admob-free';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = TabsPage;
constructor(private admobFree: AdMobFree,platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
const bannerConfig: AdMobFreeBannerConfig = {
id: 'ca-app-pub-MYCODE',
isTesting: true,
autoShow: true
};
this.admobFree.banner.config(bannerConfig);
this.admobFree.banner.show();
});
}
}
就像那个代码。
我也导入模块,将提供程序添加到 src/app/app.module.ts ;
import { AdMobFree } from '@ionic-native/admob-free';
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
providers: [
StatusBar,
SplashScreen,
AdMobFree,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
当我尝试在模拟器应用程序中工作正常但广告没有出现时。我做错了什么吗?
试试这个!
constructor(private admobFree: AdMobFree) {
this.showBannerAd(); //calling banner ads
}
async showBannerAd(){
try{
const bannerConfig: AdMobFreeBannerConfig = {
id : 'ca-app-pub-xxxx',
isTesting : true,
autoShow : true
}
this.admobFree.banner.config(bannerConfig);
const result = await this.admobFree.banner.prepare();
console.log('ad works fine', result);
}
catch(e){
console.error(e);
}
}