Skip to content

Commit 92f1af8

Browse files
PatrickJStbosch
authored andcommitted
perf(RouterLink): use hostListeners for click
with a `<router-outlet>` and lots of `router-link` you start to see noticeable lag since we’re not removing the listener Closes angular#2401
1 parent a6cb86b commit 92f1af8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

modules/angular2/src/router/router_link.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import {Location} from './location';
3333
@Directive({
3434
selector: '[router-link]',
3535
properties: ['route: routerLink', 'params: routerParams'],
36-
lifecycle: [onAllChangesDone]
36+
lifecycle: [onAllChangesDone],
37+
hostListeners: {'^click': 'onClick()'}
3738
})
3839
export class RouterLink {
3940
private _domEl;
@@ -48,16 +49,17 @@ export class RouterLink {
4849
constructor(elementRef: ElementRef, private _router: Router, private _location: Location) {
4950
this._domEl = elementRef.domElement;
5051
this._params = StringMapWrapper.create();
51-
DOM.on(this._domEl, 'click', (evt) => {
52-
DOM.preventDefault(evt);
53-
this._router.navigate(this._navigationHref);
54-
});
5552
}
5653

5754
set route(changes: string) { this._route = changes; }
5855

5956
set params(changes: StringMap<string, string>) { this._params = changes; }
6057

58+
onClick() {
59+
this._router.navigate(this._navigationHref);
60+
return false;
61+
}
62+
6163
onAllChangesDone(): void {
6264
if (isPresent(this._route) && isPresent(this._params)) {
6365
this._navigationHref = this._router.generate(this._route, this._params);

0 commit comments

Comments
 (0)