Angular 2 RC4 路由器在激活前获得预定路线 [英] Angular 2 RC4 Router get intended route before activated

查看:19
本文介绍了Angular 2 RC4 路由器在激活前获得预定路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在激活之前确定路由是什么,以便我可以缓存它并在用户登录后将用户重定向回该路由.

在旧的测试版路由器中,我可以在 activate 钩子中调用 ComponentInstruction.routeName 但在较新的 canActivate() 防护中我看不到访问的方法激活前的预定路线.

当用户在我的应用中点击导航按钮时,我可以将预期路线存储在共享服务中,但是当他们在地址栏中输入 URL 时呢?

解决方案

来自 angular router 源文件:

导出接口 CanDeactivate;{canDeactivate(组件:T,路线:激活路线快照,state:RouterStateSnapshot): Observable|布尔值;}

ActivatedRouteSnapshot 对象(路由)具有 url 属性 - 另一种类型的数组,在属性 path 您当前路径的第一个对象中.

也在 RouterStateSnapshot 对象(状态)中只有字符串属性 url 和当前路径,但带有 / 前缀.

只需将这些参数传递给您的 canDeactivate 方法 )

I am trying to determine what the route is before it is activated so I can cache it and redirect the user back to that route after they have logged in.

In the old beta router I was able to call ComponentInstruction.routeName in the activate hook but in the newer canActivate() guard I do not see a way to access the intended route before it is activated.

I could store the intended route in a shared service when the user clicks on a navagtion button in my app but what about when they enter the URL in the address bar?

解决方案

From angular router source files:

export interface CanDeactivate<T> {
  canDeactivate(component: T,
   route:ActivatedRouteSnapshot,
   state:RouterStateSnapshot): Observable<boolean> | boolean;
}

ActivatedRouteSnapshot object (route) has url property - array of another type, in first object in property path your current path.

Also inside RouterStateSnapshot object (state) has just string property url with current path, but with / prefix on it.

Simply pass those arguments to your canDeactivate method )

这篇关于Angular 2 RC4 路由器在激活前获得预定路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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