实现 Angular UI 路由器延迟解析 [英] Implementing Angular UI router lazy resolves

查看:25
本文介绍了实现 Angular UI 路由器延迟解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Angular &UI 路由器,我需要让我的父状态之一变懒惰.

I use Angular & UI router and I need for one of my parent states's resolves to be lazy.

这样做的原因是我有一个名为 authenticated 的状态,它由整个子状态层次结构继承.我需要延迟解析名为 isAuthenticated 的解析(即每次进入 authenticated 状态的子级时).

The reason for this is that I have a state called authenticated that is inherited by a whole hierarchy of child states. I need the resolve called isAuthenticated to be resolved lazily (i.e. each time a child of the authenticated state is entered).

这是我的父已认证状态:

    .state('authenticated', {
        abstract: true,
        parent: 'root',
        views: {
            'header@': {
                controller: 'NavbarCtrl',
                templateUrl: 'app/navbar/views/navbar.view.html'
            }
        },
        resolve: {
            currentMember: ['domainService', function (domainService) {
                return domainService.currentMember();
            }],
            isAuthenticated: ['$rootScope', '$q', '$cookies', function ($rootScope, $q, $cookies) {
                return ($rootScope.globals.authenticated && $cookies.globalsAuthenticated) || $q.reject({unAuthorized: true});
            }]
        }
    })

我想这样做,以便我可以轻松地将未经身份验证的用户重定向到登录页面.

I want to do that so that I can redirect unauthenticated users to login page easily.

如何使用 UI 路由器的当前实现 (0.2.15) 进行延迟解析?

How can I have a lazy resolve using the current implementation of the UI router (0.2.15)?

附言我查看了 oc 延迟加载,但它似乎旨在加载整个模块或文件...

P.S. I had a look at oc lazy load but is appears to be aimed at loading whole modules or files...

推荐答案

你可以使用 stateChangeStart 它会帮助你做你正在寻找的确切事情,检查这个链接出来

you can use stateChangeStart it will help you for doing the exact thing you are looking for , check this link out

http://brewhouse.io/blog/2014/12/09/authentication-made-simple-in-single-page-angularjs-applications.html

这篇关于实现 Angular UI 路由器延迟解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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