1
+ import { CommonModule } from '@angular/common' ;
1
2
import {
2
3
Component ,
3
4
OnInit ,
4
5
Input ,
5
6
Type ,
6
7
ViewContainerRef ,
7
- ComponentFactoryResolver ,
8
8
ContentChild ,
9
9
TemplateRef ,
10
10
ChangeDetectionStrategy ,
11
11
Self ,
12
- NgModuleFactory ,
13
- Compiler ,
14
12
OnDestroy ,
13
+ NgModuleRef ,
14
+ createNgModuleRef ,
15
15
} from '@angular/core' ;
16
16
17
17
import { Subject , BehaviorSubject , of , from } from 'rxjs' ;
@@ -24,7 +24,7 @@ import {
24
24
map ,
25
25
} from 'rxjs/operators' ;
26
26
27
- import { Load , Route , RouteOptions } from './route' ;
27
+ import { Load , ModuleWithRoute , Route , RouteOptions } from './route' ;
28
28
import { Params , RouteParams , RoutePath } from './route-params.service' ;
29
29
import { RouterComponent } from './router.component' ;
30
30
import { Router } from './router.service' ;
@@ -46,6 +46,8 @@ interface State {
46
46
@Component ( {
47
47
// tslint:disable-next-line:component-selector
48
48
selector : 'route' ,
49
+ standalone : true ,
50
+ imports : [ CommonModule ] ,
49
51
template : `
50
52
<ng-container
51
53
*ngIf="(shouldRender$ | async) && template"
@@ -110,9 +112,7 @@ export class RouteComponent implements OnInit, OnDestroy {
110
112
constructor (
111
113
private router : Router ,
112
114
private routerComponent : RouterComponent ,
113
- private resolver : ComponentFactoryResolver ,
114
- private viewContainerRef : ViewContainerRef ,
115
- private compiler : Compiler
115
+ private viewContainerRef : ViewContainerRef
116
116
) { }
117
117
118
118
ngOnInit ( ) : void {
@@ -176,32 +176,26 @@ export class RouteComponent implements OnInit, OnDestroy {
176
176
private loadAndRender ( route : Route ) {
177
177
if ( route . load ) {
178
178
return from (
179
- route . load ( ) . then ( ( componentOrModule ) => {
180
- if ( componentOrModule instanceof NgModuleFactory ) {
181
- const moduleRef = componentOrModule . create (
182
- this . viewContainerRef . injector
183
- ) ;
184
- const component = moduleRef . instance . routeComponent ;
185
-
186
- this . renderComponent ( component ) ;
187
- } else if ( componentOrModule . ɵmod ) {
188
- return this . compiler
189
- . compileModuleAsync ( componentOrModule as Type < any > )
190
- . then ( ( moduleFactory ) => {
191
- const moduleRef = moduleFactory . create (
192
- this . viewContainerRef . injector
193
- ) ;
194
- const component = moduleRef . instance . routeComponent ;
195
- this . renderComponent ( component ) ;
196
-
197
- return true ;
198
- } ) ;
199
- } else {
200
- this . renderComponent ( componentOrModule ) ;
201
- }
179
+ route
180
+ . load ( )
181
+ . then (
182
+ ( componentOrModule : NgModuleRef < ModuleWithRoute > | Type < any > ) => {
183
+ let component : Type < any > ;
184
+
185
+ if ( ( componentOrModule as any ) . ɵmod ) {
186
+ const moduleRef : NgModuleRef < ModuleWithRoute > =
187
+ createNgModuleRef (
188
+ componentOrModule as Type < any > ,
189
+ this . viewContainerRef . injector
190
+ ) ;
191
+ component = moduleRef . instance . routeComponent ;
192
+ } else {
193
+ component = componentOrModule as Type < any > ;
194
+ }
202
195
203
- return true ;
204
- } )
196
+ this . renderComponent ( component ) ;
197
+ }
198
+ )
205
199
) ;
206
200
} else {
207
201
this . showTemplate ( ) ;
@@ -210,14 +204,12 @@ export class RouteComponent implements OnInit, OnDestroy {
210
204
}
211
205
212
206
private renderComponent ( component : Type < any > ) {
213
- const componentFactory = this . resolver . resolveComponentFactory ( component ) ;
214
-
215
207
this . showTemplate ( ) ;
216
- this . viewContainerRef . createComponent (
217
- componentFactory ,
218
- this . viewContainerRef . length ,
219
- this . viewContainerRef . injector
220
- ) ;
208
+
209
+ this . viewContainerRef . createComponent ( component , {
210
+ index : this . viewContainerRef . length ,
211
+ injector : this . viewContainerRef . injector ,
212
+ } ) ;
221
213
}
222
214
223
215
private clearComponent ( ) {
0 commit comments