Something about view life clycle : ngAfterViewInit Issue

Can you please help me with an issue. Its urgent and important

(<any>window).cancelCallback = ()=>{
        debugger;
        console.log("Cancel callback was created with window instance");
        this.loader.dismiss();
    };

This is the callback method for payment. It is added to window every time I enter the view hence upon payment completion or cancellation the respective callback method is called as many number of times as I have entered that specific ts file where the callback methods are written. How should I get them added only once or remove them upon leaving the view. Kindly help as this is very urgent.

Ive tried writing this in ngAfterViewInit but it still does the same thing

From slack channel

Not sure what you’re trying to do or what payment service you are using. Could you explain more?