Skip to content

Commit 6399c2d

Browse files
committed
refactor(cdk/portal): store application in local variable to avoid repeated non-null assertion
This also has the benefit of minimizing more effectively, as the local variable can be renamed but the separate field accesses could not.
1 parent 8be9cba commit 6399c2d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/cdk/portal/dom-portal-outlet.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,22 @@ export class DomPortalOutlet extends BasePortalOutlet {
6666
if ((typeof ngDevMode === 'undefined' || ngDevMode) && !this._appRef) {
6767
throw Error('Cannot attach component portal to outlet without an ApplicationRef.');
6868
}
69+
const appRef = this._appRef!;
6970

7071
const elementInjector = portal.injector || this._defaultInjector || Injector.NULL;
71-
const environmentInjector = elementInjector.get(EnvironmentInjector, this._appRef!.injector);
72+
const environmentInjector = elementInjector.get(EnvironmentInjector, appRef.injector);
7273
componentRef = createComponent(portal.component, {
7374
elementInjector,
7475
environmentInjector,
7576
projectableNodes: portal.projectableNodes || undefined,
7677
});
7778

78-
this._appRef!.attachView(componentRef.hostView);
79+
appRef.attachView(componentRef.hostView);
7980
this.setDisposeFn(() => {
8081
// Verify that the ApplicationRef has registered views before trying to detach a host view.
8182
// This check also protects the `detachView` from being called on a destroyed ApplicationRef.
82-
if (this._appRef!.viewCount > 0) {
83-
this._appRef!.detachView(componentRef.hostView);
83+
if (appRef.viewCount > 0) {
84+
appRef.detachView(componentRef.hostView);
8485
}
8586
componentRef.destroy();
8687
});

0 commit comments

Comments
 (0)