Skip to content

Commit 70e865d

Browse files
crisbetoandrewseguin
authored andcommitted
refactor(cdk/portal): simplify DomPortalOutlet constructor (#24504)
Most of the constructor parameters of `DomPortalOutlet` are only used for component portals. These changes make them optional in order to simplify the constructor. If we hit a code path that does require them, we'll throw a runtime error. Related to the discussion in #24334. (cherry picked from commit a4642cb)
1 parent 04f4937 commit 70e865d

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@ import {BasePortalOutlet, ComponentPortal, TemplatePortal, DomPortal} from './po
2222
export class DomPortalOutlet extends BasePortalOutlet {
2323
private _document: Document;
2424

25+
/**
26+
* @param outletElement Element into which the content is projected.
27+
* @param _componentFactoryResolver Used to resolve the component factory.
28+
* Only required when attaching component portals.
29+
* @param _appRef Reference to the application. Only used in component portals when there
30+
* is no `ViewContainerRef` available.
31+
* @param _defaultInjector Injector to use as a fallback when the portal being attached doesn't
32+
* have one. Only used for component portals.
33+
* @param _document Reference to the document. Used when attaching a DOM portal. Will eventually
34+
* become a required parameter.
35+
*/
2536
constructor(
2637
/** Element into which the content is projected. */
2738
public outletElement: Element,
28-
private _componentFactoryResolver: ComponentFactoryResolver,
29-
private _appRef: ApplicationRef,
30-
private _defaultInjector: Injector,
39+
private _componentFactoryResolver?: ComponentFactoryResolver,
40+
private _appRef?: ApplicationRef,
41+
private _defaultInjector?: Injector,
3142

3243
/**
3344
* @deprecated `_document` Parameter to be made required.
@@ -45,7 +56,12 @@ export class DomPortalOutlet extends BasePortalOutlet {
4556
* @returns Reference to the created component.
4657
*/
4758
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
48-
const resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
59+
const resolver = (portal.componentFactoryResolver || this._componentFactoryResolver)!;
60+
61+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && !resolver) {
62+
throw Error('Cannot attach component portal to outlet without a ComponentFactoryResolver.');
63+
}
64+
4965
const componentFactory = resolver.resolveComponentFactory(portal.component);
5066
let componentRef: ComponentRef<T>;
5167

@@ -62,10 +78,16 @@ export class DomPortalOutlet extends BasePortalOutlet {
6278

6379
this.setDisposeFn(() => componentRef.destroy());
6480
} else {
65-
componentRef = componentFactory.create(portal.injector || this._defaultInjector);
66-
this._appRef.attachView(componentRef.hostView);
81+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && !this._appRef) {
82+
throw Error('Cannot attach component portal to outlet without an ApplicationRef.');
83+
}
84+
85+
componentRef = componentFactory.create(
86+
portal.injector || this._defaultInjector || Injector.NULL,
87+
);
88+
this._appRef!.attachView(componentRef.hostView);
6789
this.setDisposeFn(() => {
68-
this._appRef.detachView(componentRef.hostView);
90+
this._appRef!.detachView(componentRef.hostView);
6991
componentRef.destroy();
7092
});
7193
}

tools/public_api_guard/cdk/portal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class DomPortalHost extends DomPortalOutlet {
104104
// @public
105105
export class DomPortalOutlet extends BasePortalOutlet {
106106
constructor(
107-
outletElement: Element, _componentFactoryResolver: ComponentFactoryResolver, _appRef: ApplicationRef, _defaultInjector: Injector,
107+
outletElement: Element, _componentFactoryResolver?: ComponentFactoryResolver | undefined, _appRef?: ApplicationRef | undefined, _defaultInjector?: Injector | undefined,
108108
_document?: any);
109109
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T>;
110110
// @deprecated

0 commit comments

Comments
 (0)