Skip to content

Commit 9ce096e

Browse files
committed
fix: do not add ROUTER_PROVIDERS on generation.
1 parent f23bf22 commit 9ce096e

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<p>
22
<%= htmlComponentName %> Works!
33
</p>
4-
5-
<router-outlet></router-outlet>

addon/ng2/blueprints/ng2/files/src/client/app/__name__.component.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import {Component} from 'angular2/core';
2-
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router';
32

43
@Component({
54
moduleId: __moduleName,
65
selector: '<%= htmlComponentName %>-app',
7-
providers: [ROUTER_PROVIDERS],
86
templateUrl: '<%= htmlComponentName %>.component.html',
9-
styleUrls: ['<%= dasherizedModuleName %>.component.<%= styleExt %>'],
10-
directives: [ROUTER_DIRECTIVES],
11-
pipes: []
7+
styleUrls: ['<%= dasherizedModuleName %>.component.<%= styleExt %>']
128
})
13-
@RouteConfig([
14-
])
159
export class <%= jsComponentName %>App {
1610
defaultMeaning: number = 42;
1711

addon/ng2/blueprints/route/index.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/(@Component\(\{[\s\S\n]*?}\)\n)(\s*export class)/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(/(@Component\(\{)([\s\S\n]*?)(\n\}\))/m, function(_, prefix, json, suffix) {
268-
const m = json.match(/(^\s+providers:\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(/(@Component\(\{)([\s\S\n]*?)(\n\}\))/m, function (_, prefix, json, suffix) {
274+
const m = json.match(/(^\s+providers:\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+providers:\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+providers:\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(/(@Component\(\{)([\s\S\n]*?)(\}\))/m, function(_, prefix, json, suffix) {

0 commit comments

Comments
 (0)