Angular 2中用于身份验证的良好模板策略

时间:2016-05-08 13:17:16

标签: angular angular2-routing angular2-template

我目前正在运行Angular 2应用程序,如下所示:

访问网站时会引导App.component。 App.component的模板包含所有组件标记(例如menu.component,search.component和router-outlet)。

我基本上需要的是:目前访问者被直接重定向到登录页面,因为用户需要登录。他仍然能够看到菜单和仅适用于登录用户的所有组件。添加额外模板层的最佳策略是什么,因此未登录的用户会被重定向?

1 个答案:

答案 0 :(得分:5)

我这样做的方法是使用* ngIf指令“隐藏”这些元素,直到用户通过身份验证。我在上面的单词hide周围使用了引号,因为angular实际上并没有隐藏模板的那部分,它实际上根本不渲染它,所以它不在DOM中。

这意味着除非用户登录,否则只会呈现您的登录屏幕。

有关* ngIf的更多详情,请访问:

https://angular.io/docs/ts/latest/guide/structural-directives.html#!#ngIf

离。

y = []
for a in xs:
    for b in xs:
        if f(a, b):
            y.append(g(a, b))