Skip to content

fix: load pages in location instead of in appRef #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions packages/angular/src/lib/cdk/detached-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ registerElement('DetachedContainer', () => ProxyViewContainer, {
@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'DetachedContainer',
template: `<Placeholder #loader></Placeholder><ng-container #vc></ng-container><ng-content></ng-content>`,
template: `<Placeholder #loader></Placeholder>
<ng-container #vc></ng-container>
<ng-content></ng-content>`,
})
// eslint-disable-next-line @angular-eslint/component-class-suffix
export class DetachedLoader implements OnDestroy {
Expand All @@ -33,7 +35,7 @@ export class DetachedLoader implements OnDestroy {
return new TemplatePortal(templateRef, this.vc, context);
}

private loadInLocation(componentType: Type<any>): ComponentRef<any> {
private loadInAppRef(componentType: Type<any>): ComponentRef<any> {
const factory = this.resolver.resolveComponentFactory(componentType);
const componentRef = factory.create(this.containerRef.injector);
this.appRef.attachView(componentRef.hostView);
Expand All @@ -52,6 +54,10 @@ export class DetachedLoader implements OnDestroy {
return componentRef;
}

private loadInLocation(componentType: Type<any>): ComponentRef<any> {
return this.vc.createComponent(componentType);
}

public ngOnDestroy() {
this.disposeFunctions.forEach((fn) => fn());
}
Expand All @@ -65,14 +71,22 @@ export class DetachedLoader implements OnDestroy {
*/
public loadComponent(componentType: Type<any>): Promise<ComponentRef<any>> {
Trace.write('DetachedLoader.loadComponent', 'detached-loader');
return Promise.resolve(this.loadInLocation(componentType));
return Promise.resolve(this.loadInAppRef(componentType));
}

/**
* @deprecated use Portals
*/
public loadComponentSync(componentType: Type<any>): ComponentRef<any> {
Trace.write('DetachedLoader.loadComponent', 'detached-loader');
Trace.write('DetachedLoader.loadComponentSync', 'detached-loader');
return this.loadInAppRef(componentType);
}

/**
* @deprecated use Portals
*/
public loadComponentInLocation(componentType: Type<any>): ComponentRef<any> {
Trace.write('DetachedLoader.loadComponentInLocation', 'detached-loader');
return this.loadInLocation(componentType);
}

Expand All @@ -89,4 +103,11 @@ export class DetachedLoader implements OnDestroy {
});
return componentRef;
}

/**
* @deprecated use Portals
*/
public loadWithFactoryInLocation<T>(factory: ComponentFactory<T>): ComponentRef<T> {
return this.vc.createComponent(factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
loaderRef.onDestroy(() => childInjector.destroy());
this.changeDetector.markForCheck();

this.activated = loaderRef.instance.loadWithFactory(factory);
this.activated = loaderRef.instance.loadWithFactoryInLocation(factory);
this.activated.changeDetectorRef.detectChanges();
this.loadComponentInPage(page, this.activated, { activatedRoute });

Expand Down