diff --git a/nativescript-angular/router/page-router-outlet.ts b/nativescript-angular/router/page-router-outlet.ts index 174976830..5c019a1e0 100644 --- a/nativescript-angular/router/page-router-outlet.ts +++ b/nativescript-angular/router/page-router-outlet.ts @@ -64,6 +64,7 @@ export class PageRouterOutlet { // tslint:disable-line:directive-class-suffix private refCache: RefCache = new RefCache(); private isInitialPage: boolean = true; private detachedLoaderFactory: ComponentFactory; + private itemsToDestroy: CacheItem[] = []; private currentActivatedComp: ComponentRef; private currentActivatedRoute: ActivatedRoute; @@ -132,7 +133,12 @@ export class PageRouterOutlet { // tslint:disable-line:directive-class-suffix private clearRefCache() { while (this.refCache.length > 0) { - this.destroyCacheItem(this.refCache.pop()); + this.itemsToDestroy.push(this.refCache.pop()); + } + } + private destroyQueuedCacheItems() { + while (this.itemsToDestroy.length > 0) { + this.destroyCacheItem(this.itemsToDestroy.pop()); } } private destroyCacheItem(poppedItem: CacheItem) { @@ -242,7 +248,10 @@ export class PageRouterOutlet { // tslint:disable-line:directive-class-suffix // Add it to the new page page.content = componentView; - page.on("navigatedFrom", (global).Zone.current.wrap((args: NavigatedData) => { + page.on(Page.navigatedToEvent, () => setTimeout(() => { + this.destroyQueuedCacheItems(); + })); + page.on(Page.navigatedFromEvent, (global).Zone.current.wrap((args: NavigatedData) => { if (args.isBackNavigation) { this.locationStrategy._beginBackPageNavigation(); this.locationStrategy.back();