Angular2 Router CanActivate,带参数? [英] Angular2 Router CanActivate, with Parameters?

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

问题描述

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

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 类提供权限(我已将其添加到枚举中).之前我只是通过访问我的 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.

然而,我确实找到了一种解决方法,现在对我来说似乎很好.我有一个服务,我可以在其中加载我的路由模块构造.(我只是将它添加到模块的构造函数中).

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).

我的路线现在包含数据{权限:[我的值在这里]}.哪些是在 canActive 钩子中检查的,因为 canActivate 实现也使用相同的服务.

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 Cross-Module提供商/扫描

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

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

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