X Tutup
Skip to content

Commit 5e2bc5c

Browse files
committed
fix(RouterLink): ignore optional parameters when checking for active routes
fixes angular#6459 Closes angular#7834
1 parent 28e657d commit 5e2bc5c

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

modules/angular2/src/router/instruction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export class ComponentInstruction {
310310
*/
311311
constructor(public urlPath: string, public urlParams: string[], data: RouteData,
312312
public componentType, public terminal: boolean, public specificity: string,
313-
public params: {[key: string]: string} = null) {
313+
public params: {[key: string]: string} = null, public routeName: string) {
314314
this.routeData = isPresent(data) ? data : BLANK_ROUTE_DATA;
315315
}
316316
}

modules/angular2/src/router/router.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,33 @@ export class Router {
135135
*/
136136
isRouteActive(instruction: Instruction): boolean {
137137
var router: Router = this;
138+
139+
if (isBlank(this.currentInstruction)) {
140+
return false;
141+
}
142+
143+
// `instruction` corresponds to the root router
138144
while (isPresent(router.parent) && isPresent(instruction.child)) {
139145
router = router.parent;
140146
instruction = instruction.child;
141147
}
142-
return isPresent(this.currentInstruction) &&
143-
this.currentInstruction.component == instruction.component;
148+
149+
if (isBlank(instruction.component) || isBlank(this.currentInstruction.component) ||
150+
this.currentInstruction.component.routeName != instruction.component.routeName) {
151+
return false;
152+
}
153+
154+
let paramEquals = true;
155+
156+
if (isPresent(this.currentInstruction.component.params)) {
157+
StringMapWrapper.forEach(instruction.component.params, (value, key) => {
158+
if (this.currentInstruction.component.params[key] !== value) {
159+
paramEquals = false;
160+
}
161+
});
162+
}
163+
164+
return paramEquals;
144165
}
145166

146167

modules/angular2/src/router/rules/rule_set.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class RuleSet {
5959
if (config instanceof AuxRoute) {
6060
handler = new SyncRouteHandler(config.component, config.data);
6161
let routePath = this._getRoutePath(config);
62-
let auxRule = new RouteRule(routePath, handler);
62+
let auxRule = new RouteRule(routePath, handler, config.name);
6363
this.auxRulesByPath.set(routePath.toString(), auxRule);
6464
if (isPresent(config.name)) {
6565
this.auxRulesByName.set(config.name, auxRule);
@@ -85,7 +85,7 @@ export class RuleSet {
8585
useAsDefault = isPresent(config.useAsDefault) && config.useAsDefault;
8686
}
8787
let routePath = this._getRoutePath(config);
88-
let newRule = new RouteRule(routePath, handler);
88+
let newRule = new RouteRule(routePath, handler, config.name);
8989

9090
this._assertNoHashCollision(newRule.hash, config.path);
9191

modules/angular2/src/router/rules/rules.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export class RouteRule implements AbstractRule {
6969

7070
// TODO: cache component instruction instances by params and by ParsedUrl instance
7171

72-
constructor(private _routePath: RoutePath, public handler: RouteHandler) {
72+
constructor(private _routePath: RoutePath, public handler: RouteHandler,
73+
private _routeName: string) {
7374
this.specificity = this._routePath.specificity;
7475
this.hash = this._routePath.hash;
7576
this.terminal = this._routePath.terminal;
@@ -112,7 +113,7 @@ export class RouteRule implements AbstractRule {
112113
}
113114
var instruction =
114115
new ComponentInstruction(urlPath, urlParams, this.handler.data, this.handler.componentType,
115-
this.terminal, this.specificity, params);
116+
this.terminal, this.specificity, params, this._routeName);
116117
this._cache.set(hashKey, instruction);
117118

118119
return instruction;

modules/angular2/test/router/directives/router_link_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {DOM} from 'angular2/src/platform/dom/dom_adapter';
3434
import {ResolvedInstruction} from 'angular2/src/router/instruction';
3535

3636
let dummyInstruction = new ResolvedInstruction(
37-
new ComponentInstruction('detail', [], null, null, true, '0'), null, {});
37+
new ComponentInstruction('detail', [], null, null, true, '0', null, 'Detail'), null, {});
3838

3939
export function main() {
4040
describe('routerLink directive', function() {

modules/angular2/test/router/integration/router_link_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export function main() {
259259

260260
async.done();
261261
});
262-
router.navigateByUrl('/better-child');
262+
router.navigateByUrl('/better-child?extra=0');
263263
});
264264
}));
265265

@@ -300,7 +300,7 @@ export function main() {
300300

301301
async.done();
302302
});
303-
router.navigateByUrl('/child-with-grandchild/grandchild');
303+
router.navigateByUrl('/child-with-grandchild/grandchild?extra=0');
304304
});
305305
}));
306306

0 commit comments

Comments
 (0)
X Tutup