Angular2路由器可以激活,带有参数吗? [英] Angular2 Router CanActivate, with Parameters?

查看:62
本文介绍了Angular2路由器可以激活,带有参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个特定问题上看到了一些问题.

i've seen some issues on this specific issue.

我最近只从事angular2项目.无论如何,我现在正陷入一个问题.在已弃用的路由器中,我在路由的数据部分中添加了用户角色,我已经覆盖了routerOutlet,以便可以在激活路由之前检查该值.)

I'm lately only working on angular2 projects. Anyhow I'm rolling into an issue right now. In the deprecated Router I added my User Role in the data part of the route, I had overriden the routerOutlet so I could check this value before activating a route).

当前,我们可以通过可以调用的方式符合CanActivate Class/Interface行.

Currently we can conform to the CanActivate Class/Interface row w/e we can call it.

现在,我希望能够为此类CanActivate类提供Permissions(已将其击入Enum中).在我只检查用户是否通过访问SessionService登录之前,我现在需要检查用户是否具有某些权限.

Right now I would like to be able to provide Permissions (which I've slapped into an Enum) to such CanActivate classes. Before I was simply checking if the user was logged in by accessing my SessionService, I however now need to check if a user has certain permissions.

正如我现在所看到的,我必须实现一个新类,该类为每个许可都实现CanActivate.现在,这意味着我将有7个.但是,我们正处于这个确切项目的早期阶段,如果没有过多的思考,我可以将其提高到11个.

As I see it now I have to implement a new class which implements CanActivate for every single permission. Right now this would mean I'd have 7. We're however in an early stage of this exact project and Without too much thought I could improve this to 11.

在我面前可以看到的是一个实现,每个canActivate实现仅花费我大约3-5行.但是,如果我可以提供属性或参数的实现,将会给我带来更多帮助.

What I can see before me is an implementation which will only cost me about 3 - 5 lines per such canActivate implementation. However I would feel more helped by an implementation where I could provide properties or parameters.

不用考虑这种情况,我必须实现类似的类:

Not to think of the situation I'd have to implement classes like:

CanActivateIfUserHasPermissionsViewContentAndViewUsers甚至更长.

CanActivateIfUserHasPermissionsViewContentAndViewUsers or even longer.

在我的情况下,注意权限基本上是多个操作.例如查看所有内容元素,或编辑所有元素等.

Note in my situation permissions are basically multiple actions. Like View all Content elements, Or edit all of them etc.

推荐答案

所以我没有找到使用参数实现CanActivate的方法.

So I didn't find a way to implement my CanActivate with params.

但是,我确实找到了一种变通方法,现在看来对我来说很好. 我有一项服务,可以在Module构造上加载我的路由. (我只是将其添加到模块的构造函数中.)

I did however find a workaround which seems fine for me now. I have a service in which I can load my routes on Module construct. (I just add it in the constructor of the module).

我的路线"现在包含数据{权限:[此处是我的值]}.由于canActivate实现也使用相同的服务,因此可以在canActive挂钩中进行检查.

My Routes now contain data { permissions: [My values here] }. Which are checked in the canActive hook, since the canActivate implementation also uses the same service.

我的解决方案基于我对另一个问题的讨论: Angular2 RC5交叉模块提供商/扫描

My solution is based on my discussion on this other question: Angular2 RC5 Cross-Module Provider / Scanning

这篇关于Angular2路由器可以激活,带有参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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