Skip to content

Commit

Permalink
feat: use ViewContainerRef to render components
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed May 3, 2020
1 parent a0f4239 commit e4c4127
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions projects/router/src/lib/route.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
Injector,
ɵrenderComponent as renderComponent,
ɵmarkDirty as markDirty,
ɵcreateInjector as createInjector
ɵcreateInjector as createInjector,
ViewContainerRef,
ComponentFactoryResolver
} from "@angular/core";

import { Subject, BehaviorSubject, merge, of } from "rxjs";
Expand All @@ -21,9 +23,7 @@ import { RouterComponent } from "./router.component";

@Component({
selector: "route",
template: `
<div #outlet></div>
`
template: ''
})
export class RouteComponent implements OnInit {
private destroy$ = new Subject();
Expand All @@ -36,7 +36,12 @@ export class RouteComponent implements OnInit {
private _routeParams$ = new BehaviorSubject<Params>({});
routeParams$ = this._routeParams$.asObservable();

constructor(private injector: Injector, private router: RouterComponent) {}
constructor(
private injector: Injector,
private router: RouterComponent,
private resolver: ComponentFactoryResolver,
private viewContainerRef: ViewContainerRef
) {}

ngOnInit(): void {
// account for root level routes, don't add the basePath
Expand Down Expand Up @@ -73,7 +78,7 @@ export class RouteComponent implements OnInit {
.pipe(
distinctUntilChanged(),
filter(() => !!this.rendered),
tap(() => markDirty(this.rendered))
// tap(() => markDirty(this.rendered))
);

merge(activeRoute$, routeParams$).pipe(
Expand All @@ -88,28 +93,31 @@ export class RouteComponent implements OnInit {
loadAndRenderRoute(route: Route) {
if (route.loadComponent) {
return route.loadComponent().then(component => {
return this.renderView(component, this.outlet.nativeElement);
return this.renderView(component);
});
} else {
return of(this.renderView(route.component, this.outlet.nativeElement));
return of(this.renderView(route.component));
}
}

renderView(component: Type<any>, host: any) {
renderView(component: Type<any>) {
const cmpInjector = createInjector({}, this.injector, [
{ provide: RouteParams, useValue: this.routeParams$ }
]);

this.rendered = renderComponent(component, {
host,
injector: cmpInjector
});
// this.rendered = renderComponent(component, {
// host,
// injector: cmpInjector
// });
const componentFactory = this.resolver.resolveComponentFactory(component);
this.rendered = this.viewContainerRef.createComponent(componentFactory, this.viewContainerRef.length, cmpInjector);

return this.rendered;
}

clearView() {
this.outlet.nativeElement.innerHTML = "";
// this.outlet.nativeElement.innerHTML = "";
this.viewContainerRef.clear();
this.rendered = null;

return this.rendered;
Expand Down

0 comments on commit e4c4127

Please sign in to comment.