单击任何 [routerLink] 时的 Angular 2 事件 [英] Angular 2 event when any [routerLink] is clicked

查看:24
本文介绍了单击任何 [routerLink] 时的 Angular 2 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的加载器服务,可以隐藏和显示某些加载器.我正在研究一些会在连接速度较慢的情况下经常使用的东西,我需要在路线更改之间显示/隐藏加载程序.

I have a simple Loader Service that hides and shows certain loaders. I'm working on something that will be used a lot with slow connections and I need to show/hide a loader between route changes.

我可以在新路由加载时隐藏加载器.

I can hide the loader when the new route is loaded with the following.

this._Router.subscribe(() => {
    this._LoaderService.hide();
})

我正在尝试找到一种方法,当单击任何 [routerLink] 时(在路由更改开始时,而不是在结束).

I'm trying to find a way that I can call my this._LoaderService.show() function immediately when any [routerLink] is clicked (at the start of the route change, not the end).

我环顾四周,并尝试了 https://angular.io/docs/ts/latest/api/router/Router-class.html 但我似乎无法弄清楚.

I've had a look around and I tried https://angular.io/docs/ts/latest/api/router/Router-class.html but I can't seem to figure it out.

感谢任何帮助

推荐答案

您可以通过扩展默认的 https://github.com/angular/angular/blob/master/packages/router/src/directives/router_link.ts 并覆盖 onClick()

You could create your own routerLink directive by extending the default one https://github.com/angular/angular/blob/master/packages/router/src/directives/router_link.ts and override the onClick()

类似于 micronyks 的回答

Similar to micronyks answer

 <a  [routeLink]=['User'] (click)="loaderService.show()">User</a>

应该也可以,但您必须在每个地方都添加点击处理程序.

should work as well but you have to add the click handler everywhwere.

这篇关于单击任何 [routerLink] 时的 Angular 2 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆