X Tutup
Skip to content

Commit 77e8304

Browse files
committed
fix(router): do not reuse common children with different parents
1 parent 8aec215 commit 77e8304

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

modules/angular2/src/router/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export class Router {
248248
return this._outlet.canReuse(instruction.component)
249249
.then((result) => {
250250
instruction.component.reuse = result;
251-
if (isPresent(this._childRouter) && isPresent(instruction.child)) {
251+
if (result && isPresent(this._childRouter) && isPresent(instruction.child)) {
252252
return this._childRouter._canReuse(instruction.child);
253253
}
254254
});

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {RouteRegistry} from 'angular2/src/router/route_registry';
3535
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
3636

3737
var cmpInstanceCount;
38+
var childCmpInstanceCount;
3839
var log: string[];
3940

4041
export function main() {
@@ -57,6 +58,7 @@ export function main() {
5758
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
5859
tcb = tcBuilder;
5960
rtr = router;
61+
childCmpInstanceCount = 0;
6062
cmpInstanceCount = 0;
6163
log = [];
6264
}));
@@ -147,6 +149,27 @@ export function main() {
147149
});
148150
}));
149151

152+
it('should not reuse children when parent components change',
153+
inject([AsyncTestCompleter], (async) => {
154+
compile()
155+
.then((_) => rtr.config([new Route({path: '/team/:id/...', component: TeamCmp})]))
156+
.then((_) => rtr.navigate('/team/angular/user/rado'))
157+
.then((_) => {
158+
rootTC.detectChanges();
159+
expect(cmpInstanceCount).toBe(1);
160+
expect(childCmpInstanceCount).toBe(1);
161+
expect(rootTC.nativeElement).toHaveText('team angular { hello rado }');
162+
})
163+
.then((_) => rtr.navigate('/team/dart/user/rado'))
164+
.then((_) => {
165+
rootTC.detectChanges();
166+
expect(cmpInstanceCount).toBe(2);
167+
expect(childCmpInstanceCount).toBe(2);
168+
expect(rootTC.nativeElement).toHaveText('team dart { hello rado }');
169+
async.done();
170+
});
171+
}));
172+
150173
it('should inject route data into component', inject([AsyncTestCompleter], (async) => {
151174
compile()
152175
.then((_) => rtr.config([
@@ -256,7 +279,10 @@ class RouteDataCmp {
256279
@View({template: "hello {{user}}"})
257280
class UserCmp {
258281
user: string;
259-
constructor(params: RouteParams) { this.user = params.get('name'); }
282+
constructor(params: RouteParams) {
283+
childCmpInstanceCount += 1;
284+
this.user = params.get('name');
285+
}
260286
}
261287

262288

0 commit comments

Comments
 (0)
X Tutup