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

查看:182
本文介绍了单击任何[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]时(在路由更改的开始,而不是结束时),可以立即调用我的this._LoaderService.show()函数.

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.

任何帮助表示赞赏

推荐答案

您可以通过扩展默认的

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天全站免登陆