Skip to content

Commit 7705be6

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 7705be6

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
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
});

src/cdk/portal/portal.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,7 @@ describe('Portals', () => {
643643
);
644644

645645
@Component({template: ''})
646-
class ChildComponent {
647-
readonly injector = inject(Injector);
648-
}
646+
class ChildComponent {}
649647

650648
const component = createComponent(ChildComponent, {
651649
environmentInjector: childEnvironment,

0 commit comments

Comments
 (0)