fix(router): prevent calling unsubscribe on undefined subscription in RouterPreloader (#38344)

Previously, the `ngOnDestroy` method called `unsubscribe` regardless of if `subscription` had
been initialized.  This can lead to an error attempting to call `unsubscribe` of undefined.
This change prevents this error, and instead only attempts `unsubscribe` when the subscription
has been defined.

PR Close #38344
This commit is contained in:
Joey Perrott
2020-08-04 10:56:39 -07:00
committed by Andrew Kushnir
parent ba175be41f
commit 763023472b
2 changed files with 21 additions and 6 deletions

View File

@ -74,8 +74,7 @@ export class NoPreloading implements PreloadingStrategy {
@Injectable()
export class RouterPreloader implements OnDestroy {
private loader: RouterConfigLoader;
// TODO(issue/24571): remove '!'.
private subscription!: Subscription;
private subscription?: Subscription;
constructor(
private router: Router, moduleLoader: NgModuleFactoryLoader, compiler: Compiler,
@ -98,11 +97,10 @@ export class RouterPreloader implements OnDestroy {
return this.processRoutes(ngModule, this.router.config);
}
// TODO(jasonaden): This class relies on code external to the class to call setUpPreloading. If
// this hasn't been done, ngOnDestroy will fail as this.subscription will be undefined. This
// should be refactored.
ngOnDestroy(): void {
this.subscription.unsubscribe();
if (this.subscription) {
this.subscription.unsubscribe();
}
}
private processRoutes(ngModule: NgModuleRef<any>, routes: Routes): Observable<void> {