1
1
import {
2
2
Attribute , ComponentFactory , ComponentRef , Directive ,
3
3
ReflectiveInjector , ResolvedReflectiveProvider , ViewContainerRef ,
4
- Inject , ComponentResolver , provide } from '@angular/core' ;
4
+ Inject , ComponentResolver , provide , ComponentFactoryResolver ,
5
+ NoComponentFactoryError } from '@angular/core' ;
5
6
6
7
import { isBlank , isPresent } from '@angular/core/src/facade/lang' ;
7
8
8
- import { RouterOutletMap , ActivatedRoute , PRIMARY_OUTLET } from '@angular/router' ;
9
- import { RouterOutlet } from '@angular/router/directives/router_outlet' ;
9
+ import { RouterOutletMap , ActivatedRoute , RouterOutlet , PRIMARY_OUTLET } from '@angular/router' ;
10
10
import { NSLocationStrategy } from "./ns-location-strategy" ;
11
11
import { DEVICE } from "../platform-providers" ;
12
12
import { Device } from "platform" ;
@@ -40,10 +40,8 @@ class RefCache {
40
40
}
41
41
}
42
42
43
-
44
-
45
43
@Directive ( { selector : 'page-router-outlet' } )
46
- export class PageRouterOutlet extends RouterOutlet {
44
+ export class PageRouterOutlet {
47
45
private viewUtil : ViewUtil ;
48
46
private refCache : RefCache = new RefCache ( ) ;
49
47
private isInitalPage : boolean = true ;
@@ -52,6 +50,8 @@ export class PageRouterOutlet extends RouterOutlet {
52
50
private currnetActivatedComp : ComponentRef < any > ;
53
51
private currentActivatedRoute : ActivatedRoute ;
54
52
53
+ public outletMap : RouterOutletMap ;
54
+
55
55
get isActivated ( ) : boolean {
56
56
return ! ! this . currnetActivatedComp ;
57
57
}
@@ -76,15 +76,17 @@ export class PageRouterOutlet extends RouterOutlet {
76
76
private containerRef : ViewContainerRef ,
77
77
@Attribute ( 'name' ) name : string ,
78
78
private locationStrategy : NSLocationStrategy ,
79
+ private componentFactoryResolver : ComponentFactoryResolver ,
79
80
compiler : ComponentResolver ,
80
81
@Inject ( DEVICE ) device : Device ) {
81
- super ( parentOutletMap , containerRef , name )
82
+
83
+ parentOutletMap . registerOutlet ( name ? name : PRIMARY_OUTLET , < any > this ) ;
82
84
83
85
this . viewUtil = new ViewUtil ( device ) ;
84
86
compiler . resolveComponent ( DetachedLoader ) . then ( ( detachedLoaderFactory ) => {
85
87
log ( "DetachedLoaderFactory leaded" ) ;
86
88
this . detachedLoaderFactory = detachedLoaderFactory ;
87
- } )
89
+ } ) ;
88
90
}
89
91
90
92
deactivate ( ) : void {
@@ -115,7 +117,6 @@ export class PageRouterOutlet extends RouterOutlet {
115
117
* This method in turn is responsible for calling the `routerOnActivate` hook of its child.
116
118
*/
117
119
activate (
118
- factory : ComponentFactory < any > ,
119
120
activatedRoute : ActivatedRoute ,
120
121
providers : ResolvedReflectiveProvider [ ] ,
121
122
outletMap : RouterOutletMap ) : void {
@@ -124,16 +125,16 @@ export class PageRouterOutlet extends RouterOutlet {
124
125
this . currentActivatedRoute = activatedRoute ;
125
126
126
127
if ( this . locationStrategy . isPageNavigatingBack ( ) ) {
127
- this . activateOnGoBack ( factory , activatedRoute , providers ) ;
128
+ this . activateOnGoBack ( activatedRoute , providers ) ;
128
129
} else {
129
- this . activateOnGoForward ( factory , activatedRoute , providers ) ;
130
+ this . activateOnGoForward ( activatedRoute , providers ) ;
130
131
}
131
132
}
132
133
133
134
private activateOnGoForward (
134
- factory : ComponentFactory < any > ,
135
135
activatedRoute : ActivatedRoute ,
136
136
providers : ResolvedReflectiveProvider [ ] ) : void {
137
+ const factory = this . getComponentFactory ( activatedRoute ) ;
137
138
138
139
if ( this . isInitalPage ) {
139
140
log ( "PageRouterOutlet.activate() inital page - just load component: " + activatedRoute . component ) ;
@@ -146,7 +147,7 @@ export class PageRouterOutlet extends RouterOutlet {
146
147
log ( "PageRouterOutlet.activate() forward navigation - create detached loader in the loader container: " + activatedRoute . component ) ;
147
148
148
149
const page = new Page ( ) ;
149
- const pageResolvedProvider = ReflectiveInjector . resolve ( [ provide ( Page , { useValue : page } ) ] )
150
+ const pageResolvedProvider = ReflectiveInjector . resolve ( [ provide ( Page , { useValue : page } ) ] ) ;
150
151
const childInjector = ReflectiveInjector . fromResolvedProviders ( [ ...providers , ...pageResolvedProvider ] , this . containerRef . parentInjector ) ;
151
152
const loaderRef = this . containerRef . createComponent ( this . detachedLoaderFactory , this . containerRef . length , childInjector , [ ] ) ;
152
153
@@ -156,7 +157,7 @@ export class PageRouterOutlet extends RouterOutlet {
156
157
}
157
158
}
158
159
159
- private activateOnGoBack ( factory : ComponentFactory < any > ,
160
+ private activateOnGoBack (
160
161
activatedRoute : ActivatedRoute ,
161
162
providers : ResolvedReflectiveProvider [ ] ) : void {
162
163
log ( "PageRouterOutlet.activate() - Back naviation, so load from cache: " + activatedRoute . component ) ;
@@ -189,11 +190,35 @@ export class PageRouterOutlet extends RouterOutlet {
189
190
create : ( ) => { return page ; }
190
191
} ) ;
191
192
}
193
+
194
+ // NOTE: Using private APIs - potential break point!
195
+ private getComponentFactory ( activatedRoute : any ) : ComponentFactory < any > {
196
+ const snapshot = activatedRoute . _futureSnapshot ;
197
+ const component : any = < any > snapshot . _routeConfig . component ;
198
+ let factory : ComponentFactory < any > ;
199
+ try {
200
+ factory = typeof component === 'string' ?
201
+ snapshot . _resolvedComponentFactory :
202
+ this . componentFactoryResolver . resolveComponentFactory ( component ) ;
203
+ } catch ( e ) {
204
+ if ( ! ( e instanceof NoComponentFactoryError ) ) {
205
+ throw e ;
206
+ }
207
+ // TODO: vsavkin uncomment this once ComponentResolver is deprecated
208
+ // const componentName = component ? component.name : null;
209
+ // console.warn(
210
+ // `'${componentName}' not found in precompile array. To ensure all components referred
211
+ // to by the RouterConfig are compiled, you must add '${componentName}' to the
212
+ // 'precompile' array of your application component. This will be required in a future
213
+ // release of the router.`);
214
+
215
+ factory = snapshot . _resolvedComponentFactory ;
216
+ }
217
+ return factory ;
218
+ }
219
+
192
220
}
193
221
194
222
function log ( msg : string ) {
195
223
routerLog ( msg ) ;
196
- }
197
-
198
-
199
-
224
+ }
0 commit comments