@@ -25,12 +25,17 @@ export class PageRoute {
2525 }
2626}
2727
28+ function isComponentFactoryResolver ( item : any ) : item is ComponentFactoryResolver {
29+ return ! ! item . resolveComponentFactory ;
30+ }
31+
2832export class DestructibleInjector implements Injector {
2933 private refs = new Set < any > ( ) ;
30- constructor ( private destructableProviders : ProviderSet , private parent : Injector ) { }
34+ constructor ( private destructibleProviders : ProviderSet , private parent : Injector ) { }
3135 get < T > ( token : Type < T > | InjectionToken < T > , notFoundValue ?: T , flags ?: InjectFlags ) : T {
3236 const ref = this . parent . get ( token , notFoundValue , flags ) ;
33- if ( this . destructableProviders . has ( token ) ) {
37+ // if we're skipping ourselves then it's not our responsibility to destroy
38+ if ( ! ( flags & InjectFlags . SkipSelf ) && this . destructibleProviders . has ( token ) ) {
3439 this . refs . add ( ref ) ;
3540 }
3641 return ref ;
@@ -131,7 +136,8 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
131136 private ngZone : NgZone ,
132137 private router : Router ,
133138 elRef : ElementRef ,
134- viewUtil : ViewUtil
139+ viewUtil : ViewUtil ,
140+ private environmentInjector : EnvironmentInjector
135141 ) {
136142 this . isEmptyOutlet = isEmptyOutlet ;
137143 this . frame = elRef . nativeElement ;
@@ -310,34 +316,24 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
310316
311317 this . markActivatedRoute ( activatedRoute ) ;
312318
313- resolver = resolver || this . resolver ;
314-
315- this . activateOnGoForward ( activatedRoute , resolver ) ;
319+ this . activateOnGoForward ( activatedRoute , resolver || this . environmentInjector ) ;
316320 this . activateEvents . emit ( this . activated . instance ) ;
317321 }
318322
319- private activateOnGoForward ( activatedRoute : ActivatedRoute , loadedResolver : ComponentFactoryResolver | EnvironmentInjector ) : void {
323+ private activateOnGoForward ( activatedRoute : ActivatedRoute , resolverOrInjector : ComponentFactoryResolver | EnvironmentInjector ) : void {
320324 if ( NativeScriptDebug . isLogEnabled ( ) ) {
321325 NativeScriptDebug . routerLog ( 'PageRouterOutlet.activate() forward navigation - ' + 'create detached loader in the loader container' ) ;
322326 }
323327
324- let resolver : ComponentFactoryResolver ;
325- let ourInjector = this . location . injector ;
326- if ( ! ( loadedResolver instanceof ComponentFactoryResolver ) ) {
327- ourInjector = loadedResolver ;
328- resolver = loadedResolver ?. get ( ComponentFactoryResolver ) ;
329- } else {
330- resolver = loadedResolver ;
331- }
332-
333- const factory = this . getComponentFactory ( activatedRoute , resolver ) ;
328+ const component = this . getComponentType ( activatedRoute ) ;
334329 const page = this . pageFactory ( {
335330 isNavigation : true ,
336- componentType : factory . componentType ,
331+ componentType : component ,
337332 } ) ;
333+ const location = this . location ;
338334
339- const destructables = new Set ( [ ] ) ;
340- const injector = Injector . create ( {
335+ const destructibles = new Set ( [ PageService ] ) ;
336+ const parentInjector = Injector . create ( {
341337 providers : [
342338 { provide : Page , useValue : page } ,
343339 { provide : Frame , useValue : this . frame } ,
@@ -346,15 +342,22 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
346342 { provide : ChildrenOutletContexts , useValue : this . parentContexts . getOrCreateContext ( this . name ) . children } ,
347343 { provide : PageService , useClass : PageService } ,
348344 ] ,
349- parent : ourInjector ,
345+ parent : location . injector ,
350346 } ) ;
351347
352- const childInjector = new DestructibleInjector ( destructables , injector ) ;
353- const loaderRef = this . location . createComponent ( this . detachedLoaderFactory , this . location . length , childInjector , [ ] ) ;
354- loaderRef . onDestroy ( ( ) => childInjector . destroy ( ) ) ;
348+ const injector = new DestructibleInjector ( destructibles , parentInjector ) ;
349+ let loaderRef : ComponentRef < DetachedLoader > ;
350+ if ( isComponentFactoryResolver ( resolverOrInjector ) ) {
351+ const factory = resolverOrInjector . resolveComponentFactory ( DetachedLoader ) ;
352+ loaderRef = location . createComponent ( factory , location . length , injector ) ;
353+ } else {
354+ const environmentInjector = resolverOrInjector ;
355+ loaderRef = location . createComponent ( DetachedLoader , { index : location . length , injector, environmentInjector } ) ;
356+ }
357+ loaderRef . onDestroy ( ( ) => injector . destroy ( ) ) ;
355358 this . changeDetector . markForCheck ( ) ;
356359
357- this . activated = loaderRef . instance . loadWithFactoryInLocation ( factory ) ;
360+ this . activated = loaderRef . instance . loadComponentInLocation ( component ) ;
358361 this . activated . changeDetectorRef . detectChanges ( ) ;
359362 this . loadComponentInPage ( page , this . activated , { activatedRoute } ) ;
360363
@@ -482,10 +485,8 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
482485 }
483486 }
484487
485- private getComponentFactory ( activatedRoute : ActivatedRoute , loadedResolver : ComponentFactoryResolver ) : ComponentFactory < any > {
486- const component = activatedRoute . routeConfig . component || activatedRoute . component ;
487-
488- return loadedResolver ? loadedResolver . resolveComponentFactory ( component ) : this . componentFactoryResolver . resolveComponentFactory ( component ) ;
488+ private getComponentType ( activatedRoute : ActivatedRoute ) : Type < any > {
489+ return activatedRoute . routeConfig . component || activatedRoute . component ;
489490 }
490491
491492 private getOutlet ( activatedRouteSnapshot : ActivatedRouteSnapshot ) : Outlet {
0 commit comments