Skip to content

Commit edc7d57

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

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
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: 23 additions & 19 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,8 @@ module.exports = {
214213
return;
215214
}
216215

216+
const isAppComponent = (parentFile == path.join(this.dynamicPath.dir, this.project.name() + '.component.ts'));
217+
217218
const jsComponentName = stringUtils.classify(options.entity.name);
218219
const base = parsedPath.base;
219220

@@ -263,25 +264,28 @@ module.exports = {
263264
}
264265
});
265266

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-
}
267+
// Add the provider, only on the APP itself.
268+
if (isAppComponent) {
269+
content = _insertImport(content, 'ROUTER_DIRECTIVES', 'angular2/router');
270+
content = content.replace(/(@Component\(\{)([\s\S\n]*?)(\n\}\))/m, function (_, prefix, json, suffix) {
271+
const m = json.match(/(^\s+providers:\s*\[)([\s\S\n]*)(\]\s*,?.*$)/m);
272+
if (m) {
273+
if (m[2].indexOf('ROUTER_PROVIDERS') != -1) {
274+
// Already there.
275+
return _;
276+
}
274277

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-
});
278+
// There's a directive already, but no ROUTER_PROVIDERS.
279+
return prefix +
280+
json.replace(/(^\s+providers:\s*\[)([\s\S\n]*)(^\]\s*,?.*$)/m, function (_, prefix, d, suffix) {
281+
return prefix + d + (d ? ',' : '') + 'ROUTER_PROVIDERS' + suffix;
282+
}) + suffix;
283+
} else {
284+
// There's no directive already.
285+
return prefix + json + ',\n providers: [ROUTER_PROVIDERS]' + suffix;
286+
}
287+
});
288+
}
285289

286290
// Change the template.
287291
content = content.replace(/(@Component\(\{)([\s\S\n]*?)(\}\))/m, function(_, prefix, json, suffix) {

0 commit comments

Comments
 (0)