X Tutup
Skip to content

Commit c603643

Browse files
Alexander BachmannIgorMinar
authored andcommitted
fix(router): don't prepend / unnecessarily to Location paths
Closes #6729 Closes #5502
1 parent d86be24 commit c603643

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

modules/angular1_router/test/integration/navigation_spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ describe('navigation', function () {
131131
expect(elt.text()).toBe('outer { inner { one } }');
132132
});
133133

134+
it('should work when parent route has empty path', inject(function ($location) {
135+
registerComponent('childCmp', {
136+
template: '<div>inner { <div ng-outlet></div> }</div>',
137+
$routeConfig: [
138+
{ path: '/b', component: 'oneCmp' }
139+
]
140+
});
141+
142+
$router.config([
143+
{ path: '/...', component: 'childCmp' }
144+
]);
145+
compile('<div>outer { <div ng-outlet></div> }</div>');
146+
147+
$router.navigateByUrl('/b');
148+
$rootScope.$digest();
149+
150+
expect(elt.text()).toBe('outer { inner { one } }');
151+
expect($location.path()).toBe('/b');
152+
}));
153+
134154

135155
it('should work with recursive nested outlets', function () {
136156
registerDirective('recurCmp', {

modules/angular2/src/router/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export class RootRouter extends Router {
438438
}
439439
var emitPath = instruction.toUrlPath();
440440
var emitQuery = instruction.toUrlQuery();
441-
if (emitPath.length > 0) {
441+
if (emitPath.length > 0 && emitPath[0] != '/') {
442442
emitPath = '/' + emitPath;
443443
}
444444

@@ -465,7 +465,7 @@ export class RootRouter extends Router {
465465
commit(instruction: Instruction, _skipLocationChange: boolean = false): Promise<any> {
466466
var emitPath = instruction.toUrlPath();
467467
var emitQuery = instruction.toUrlQuery();
468-
if (emitPath.length > 0) {
468+
if (emitPath.length > 0 && emitPath[0] != '/') {
469469
emitPath = '/' + emitPath;
470470
}
471471
var promise = super.commit(instruction);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ export function main() {
105105
});
106106
}));
107107

108+
it('should navigate to child routes when the root component has an empty path',
109+
inject([AsyncTestCompleter, Location], (async, location) => {
110+
compile(tcb, 'outer { <router-outlet></router-outlet> }')
111+
.then((rtc) => {fixture = rtc})
112+
.then((_) => rtr.config([new Route({path: '/...', component: ParentCmp})]))
113+
.then((_) => rtr.navigateByUrl('/b'))
114+
.then((_) => {
115+
fixture.detectChanges();
116+
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
117+
expect(location.urlChanges).toEqual(['/b']);
118+
async.done();
119+
});
120+
}));
121+
108122
it('should navigate to child routes of async routes', inject([AsyncTestCompleter], (async) => {
109123
compile(tcb, 'outer { <router-outlet></router-outlet> }')
110124
.then((rtc) => {fixture = rtc})

0 commit comments

Comments
 (0)
X Tutup