Problem with events in differents components


#1

Hi guys,

I have this:

pets.ts PetsPage

import { AdMobPro } from '@ionic-native/admob-pro';

@Component({
    selector: 'page-pets',
    templateUrl: 'pets.html'
})
export class PetsPage {
    constructor(
        private navCtrol: NavController,
        private admob: AdMobPro,
        private app: App,
    ) {   }

   ionViewDidLoad() {
    this.admob.onAdPresent()
        .subscribe((data) => {
            if (data.adType == 'rewardvideo') {
                    alert('aqui');
        }
    });
   };

 addPet() {
        this.navCtrol.push('PetPage');
    }

PetPage Pet.ts


import { AdMobPro } from '@ionic-native/admob-pro';

@Component({
    selector: 'page-pet',
    templateUrl: 'pet.html'
})
export class PetPage implements OnInit {

    private todo: FormGroup;
    submitAttempt: boolean = false;

    isPet: boolean = true; //variable q se usa cuando insertamos imagenes
    pet = {} as Pet;

    listOfValuesType: ListOfValues[] = [];
    //listOfValuesRaza: ListOfValues[] = [];

    constructor(
        private admob: AdMobPro) {    }

    ionViewDidLoad() {
        this.admob.onAdPresent()
            .subscribe((data) => {
                //rewardType
                if (data.adType == 'rewardvideo') {
                }
            });

    }

sharedPet() {
        this.admob.prepareRewardVideoAd({
            adId: 'ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx',
            autoShow: true
        });

    }

WIch one have ur module with your providers

import { AdMobPro } from '@ionic-native/admob-pro';


@NgModule({
    declarations: [PetsPage],
    providers: [AdMobPro ],
    imports: [
        IonicPageModule.forChild(PetsPage)
    ]
})

export class PetsPageModule{}
import { AdMobPro } from '@ionic-native/admob-pro';

@NgModule({
    declarations: [PetPage],
    providers:[IAdMobPro],
    imports: [
        IonicPageModule.forChild(PetPage)
    ]
})

export class PetPageModule{}

1 - I will go to call the function addPet
2 - Call the function sharedPet

My problem is that I don’t know why the event onAdPresent run in the two components when I invoke the function sharedPet in the second component (PetPage). The function sharedPet have an actionthat active the event onAdPresent,
but I do not understand why the event is invoked in the PetsPage component, because PetsPage and PetPage have differents modules and provider.

Any idea?

Thanks


#2

Hi,

The provider is the same, if you provider emitt a event, all the component that have the provider and are subscribed are going to receive the event.


#3

I disagree with the previous post, because you have (attempted to) explicitly have separate provider instances for these two pages.

I’ve never used this plugin, but a quick look at the ionic-native source suggests to me that there isn’t really any simple way for you to get around this, as what is happening under the hood is that the ionic-native providers are all responding to the same global event that is happening on document. I don’t think you can disambiguate them unless there is some way to do so by putting a cookie into whatever data comes with these events.


#4

:frowning: Arg, thank you any way