@@ -79,7 +79,6 @@ function _addRouteConfig(content) {
7979 // Add the imports.
8080 content = _insertImport ( content , 'RouteConfig' , 'angular2/router' ) ;
8181 content = _insertImport ( content , 'ROUTER_DIRECTIVES' , 'angular2/router' ) ;
82- content = _insertImport ( content , 'ROUTER_PROVIDERS' , 'angular2/router' ) ;
8382
8483 // Add the router config.
8584 const m = content . match ( / ( @ C o m p o n e n t \( \{ [ \s \S \n ] * ?} \) \n ) ( \s * e x p o r t c l a s s ) / m) ;
@@ -214,6 +213,11 @@ module.exports = {
214213 return ;
215214 }
216215
216+ let isAppComponent = false ;
217+ if ( parentFile == path . join ( this . dynamicPath . dir , this . project . name ( ) + '.component.ts' ) ) {
218+ isAppComponent = true ;
219+ }
220+
217221 const jsComponentName = stringUtils . classify ( options . entity . name ) ;
218222 const base = parsedPath . base ;
219223
@@ -223,9 +227,9 @@ module.exports = {
223227 `./${ options . isLazyRoute ? '+' : '' } ${ base } ` ) ;
224228
225229 let defaultReg = options . default ? ', useAsDefault: true' : '' ;
226- let path = options . path || `/${ base } ` ;
230+ let routePath = options . path || `/${ base } ` ;
227231 let route = '{'
228- + `path: '${ path } ', `
232+ + `path: '${ routePath } ', `
229233 + `name: '${ jsComponentName } ', `
230234 + `component: ${ jsComponentName } Component`
231235 + defaultReg
@@ -263,25 +267,28 @@ module.exports = {
263267 }
264268 } ) ;
265269
266- // Add the provider.
267- content = content . replace ( / ( @ C o m p o n e n t \( \{ ) ( [ \s \S \n ] * ?) ( \n \} \) ) / m, function ( _ , prefix , json , suffix ) {
268- const m = json . match ( / ( ^ \s + p r o v i d e r s : \s * \[ ) ( [ \s \S \n ] * ) ( \] \s * , ? .* $ ) / m) ;
269- if ( m ) {
270- if ( m [ 2 ] . indexOf ( 'ROUTER_PROVIDERS' ) != - 1 ) {
271- // Already there.
272- return _ ;
273- }
270+ // Add the provider, only on the APP itself.
271+ if ( isAppComponent ) {
272+ content = _insertImport ( content , 'ROUTER_DIRECTIVES' , 'angular2/router' ) ;
273+ content = content . replace ( / ( @ C o m p o n e n t \( \{ ) ( [ \s \S \n ] * ?) ( \n \} \) ) / m, function ( _ , prefix , json , suffix ) {
274+ const m = json . match ( / ( ^ \s + p r o v i d e r s : \s * \[ ) ( [ \s \S \n ] * ) ( \] \s * , ? .* $ ) / m) ;
275+ if ( m ) {
276+ if ( m [ 2 ] . indexOf ( 'ROUTER_PROVIDERS' ) != - 1 ) {
277+ // Already there.
278+ return _ ;
279+ }
274280
275- // There's a directive already, but no ROUTER_PROVIDERS.
276- return prefix +
277- json . replace ( / ( ^ \s + p r o v i d e r s : \s * \[ ) ( [ \s \S \n ] * ) ( ^ \] \s * , ? .* $ ) / m, function ( _ , prefix , d , suffix ) {
278- return prefix + d + ( d ? ',' : '' ) + 'ROUTER_PROVIDERS' + suffix ;
279- } ) + suffix ;
280- } else {
281- // There's no directive already.
282- return prefix + json + ',\n providers: [ROUTER_PROVIDERS]' + suffix ;
283- }
284- } ) ;
281+ // There's a directive already, but no ROUTER_PROVIDERS.
282+ return prefix +
283+ json . replace ( / ( ^ \s + p r o v i d e r s : \s * \[ ) ( [ \s \S \n ] * ) ( ^ \] \s * , ? .* $ ) / m, function ( _ , prefix , d , suffix ) {
284+ return prefix + d + ( d ? ',' : '' ) + 'ROUTER_PROVIDERS' + suffix ;
285+ } ) + suffix ;
286+ } else {
287+ // There's no directive already.
288+ return prefix + json + ',\n providers: [ROUTER_PROVIDERS]' + suffix ;
289+ }
290+ } ) ;
291+ }
285292
286293 // Change the template.
287294 content = content . replace ( / ( @ C o m p o n e n t \( \{ ) ( [ \s \S \n ] * ?) ( \} \) ) / m, function ( _ , prefix , json , suffix ) {
0 commit comments