fix(router): support guards navigating synchronously (#11150)
This commit is contained in:
		
							parent
							
								
									e8a1566065
								
							
						
					
					
						commit
						e2241a2f92
					
				| @ -559,9 +559,12 @@ export class Router { | |||||||
|                     rejectPromise(ee); |                     rejectPromise(ee); | ||||||
|                   } |                   } | ||||||
|                 } |                 } | ||||||
|  | 
 | ||||||
|  |                 if (id === this.navigationId) { | ||||||
|                   this.currentRouterState = storedState; |                   this.currentRouterState = storedState; | ||||||
|                   this.currentUrlTree = storedUrl; |                   this.currentUrlTree = storedUrl; | ||||||
|                   this.location.replaceState(this.serializeUrl(storedUrl)); |                   this.location.replaceState(this.serializeUrl(storedUrl)); | ||||||
|  |                 } | ||||||
|               }); |               }); | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -1227,7 +1227,14 @@ describe('Integration', () => { | |||||||
|         beforeEach(() => { |         beforeEach(() => { | ||||||
|           TestBed.configureTestingModule({ |           TestBed.configureTestingModule({ | ||||||
|             providers: [ |             providers: [ | ||||||
|               {provide: 'alwaysFalse', useValue: (a: any) => false}, |               {provide: 'alwaysFalse', useValue: (a: any) => false}, { | ||||||
|  |                 provide: 'returnFalseAndNavigate', | ||||||
|  |                 useFactory: (router: any) => (a: any) => { | ||||||
|  |                   router.navigate(['blank']); | ||||||
|  |                   return false; | ||||||
|  |                 }, | ||||||
|  |                 deps: [Router] | ||||||
|  |               }, | ||||||
|               {provide: 'alwaysTrue', useValue: (a: any) => true} |               {provide: 'alwaysTrue', useValue: (a: any) => true} | ||||||
|             ] |             ] | ||||||
|           }); |           }); | ||||||
| @ -1275,7 +1282,6 @@ describe('Integration', () => { | |||||||
| 
 | 
 | ||||||
|                  recordedEvents.splice(0); |                  recordedEvents.splice(0); | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|                  // successful navigation
 |                  // successful navigation
 | ||||||
|                  router.navigateByUrl('/lazyTrue/loaded'); |                  router.navigateByUrl('/lazyTrue/loaded'); | ||||||
|                  advance(fixture); |                  advance(fixture); | ||||||
| @ -1287,6 +1293,32 @@ describe('Integration', () => { | |||||||
|                    [NavigationEnd, '/lazyTrue/loaded'] |                    [NavigationEnd, '/lazyTrue/loaded'] | ||||||
|                  ]); |                  ]); | ||||||
|                }))); |                }))); | ||||||
|  | 
 | ||||||
|  |         it('should support navigating from within the guard', | ||||||
|  |            fakeAsync(inject([Router, Location], (router: Router, location: Location) => { | ||||||
|  | 
 | ||||||
|  |              const fixture = createRoot(router, RootCmp); | ||||||
|  | 
 | ||||||
|  |              router.resetConfig([ | ||||||
|  |                {path: 'lazyFalse', canLoad: ['returnFalseAndNavigate'], loadChildren: 'lazyFalse'}, | ||||||
|  |                {path: 'blank', component: BlankCmp} | ||||||
|  |              ]); | ||||||
|  | 
 | ||||||
|  |              const recordedEvents: any[] = []; | ||||||
|  |              router.events.forEach(e => recordedEvents.push(e)); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |              router.navigateByUrl('/lazyFalse/loaded'); | ||||||
|  |              advance(fixture); | ||||||
|  | 
 | ||||||
|  |              expect(location.path()).toEqual('/blank'); | ||||||
|  | 
 | ||||||
|  |              expectEvents(recordedEvents, [ | ||||||
|  |                [NavigationStart, '/lazyFalse/loaded'], [NavigationStart, '/blank'], | ||||||
|  |                [RoutesRecognized, '/blank'], [NavigationCancel, '/lazyFalse/loaded'], | ||||||
|  |                [NavigationEnd, '/blank'] | ||||||
|  |              ]); | ||||||
|  |            }))); | ||||||
|       }); |       }); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user