Skip to content

Commit 390cfb7

Browse files
committed
fix(router): generate links for router-link with baseHref
1 parent 17392f6 commit 390cfb7

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

modules/angular2/src/mock/location_mock.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ export class SpyLocation extends SpyObject {
1212
urlChanges:List<string>;
1313
_path:string;
1414
_subject:EventEmitter;
15+
_baseHref:string;
1516

1617
constructor() {
1718
super();
1819
this._path = '/';
1920
this.urlChanges = ListWrapper.create();
2021
this._subject = new EventEmitter();
22+
this._baseHref = '';
2123
}
2224

2325
setInitialPath(url:string) {
2426
this._path = url;
2527
}
2628

29+
setBaseHref(url:string) {
30+
this._baseHref = url;
31+
}
32+
2733
path():string {
2834
return this._path;
2935
}
@@ -34,6 +40,10 @@ export class SpyLocation extends SpyObject {
3440
});
3541
}
3642

43+
normalizeAbsolutely(url) {
44+
return this._baseHref + url;
45+
}
46+
3747
go(url:string) {
3848
if (this._path === url) {
3949
return;

modules/angular2/src/router/location.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,29 @@ export class Location {
2727
return this._stripBaseHref(stripIndexHtml(url));
2828
}
2929

30+
normalizeAbsolutely(url) {
31+
if (url[0] != '/') {
32+
url = '/' + url;
33+
}
34+
return this._addBaseHref(url);
35+
}
36+
3037
_stripBaseHref(url) {
3138
if (this._baseHref.length > 0 && StringWrapper.startsWith(url, this._baseHref)) {
3239
return StringWrapper.substring(url, this._baseHref.length);
3340
}
3441
return url;
3542
}
3643

44+
_addBaseHref(url) {
45+
if (!StringWrapper.startsWith(url, this._baseHref)) {
46+
return this._baseHref + url;
47+
}
48+
return url;
49+
}
50+
3751
go(url:string) {
38-
var finalUrl = url[0] == '/' ? url : this._baseHref + '/' + url;
52+
var finalUrl = this.normalizeAbsolutely(url);
3953
this._browserLocation.pushState(null, '', finalUrl);
4054
}
4155

modules/angular2/src/router/router_link.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {isPresent} from 'angular2/src/facade/lang';
66
import {DOM} from 'angular2/src/dom/dom_adapter';
77

88
import {Router} from './router';
9+
import {Location} from './location';
910

1011
/**
1112
* The RouterLink directive lets you link to specific parts of your app.
@@ -41,11 +42,13 @@ export class RouterLink {
4142
_route:string;
4243
_params:any;
4344
_router:Router;
45+
_location:Location;
4446
_href:string;
4547

46-
constructor(elementRef:ElementRef, router:Router) {
48+
constructor(elementRef:ElementRef, router:Router, location:Location) {
4749
this._domEl = elementRef.domElement;
4850
this._router = router;
51+
this._location = location;
4952
this._params = StringMapWrapper.create();
5053
DOM.on(this._domEl, 'click', (evt) => {
5154
evt.preventDefault();
@@ -64,10 +67,10 @@ export class RouterLink {
6467
onAllChangesDone() {
6568
if (isPresent(this._route) && isPresent(this._params)) {
6669
var newHref = this._router.generate(this._route, this._params);
67-
this._href = newHref;
70+
this._href = this._location.normalizeAbsolutely(newHref);
6871
// Keeping the link on the element to support contextual menu `copy link`
6972
// and other in-browser affordances.
70-
DOM.setAttribute(this._domEl, 'href', newHref);
73+
DOM.setAttribute(this._domEl, 'href', this._href);
7174
}
7275
}
7376
}

modules/angular2/test/router/outlet_spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ export function main() {
136136
}));
137137

138138

139+
it('should generate absolute hrefs that include the base href', inject([AsyncTestCompleter], (async) => {
140+
location.setBaseHref('/my/base');
141+
compile('<a href="hello" router-link="user"></a>')
142+
.then((_) => rtr.config({'path': '/user', 'component': UserCmp, 'as': 'user'}))
143+
.then((_) => rtr.navigate('/a/b'))
144+
.then((_) => {
145+
view.detectChanges();
146+
expect(DOM.getAttribute(view.rootNodes[0].childNodes[0], 'href')).toEqual('/my/base/user');
147+
async.done();
148+
});
149+
}));
150+
151+
139152
it('should generate link hrefs without params', inject([AsyncTestCompleter], (async) => {
140153
compile('<a href="hello" router-link="user"></a>')
141154
.then((_) => rtr.config({'path': '/user', 'component': UserCmp, 'as': 'user'}))

0 commit comments

Comments
 (0)