角$ 1.5 routerOnActivate对根路由器 [英] Angular 1.5 $routerOnActivate on Root Router

查看:667
本文介绍了角$ 1.5 routerOnActivate对根路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要确保用户在继续进行任何组件的他之前正确/她的努力达到,如果他们没有登录记录下来。给他们登录。

I want to make sure that the user is logged in properly before proceeding to any of the components he/she's trying to reach, if they're not logged in. Send them to login.

我的想法是在根路由器上做了 $ routerOnActivate 的支票。这对我来说会解决所有子路径的问题。

My idea is to do a check in the $routerOnActivate in the root router. Which to me would solve the issue for any sub routes.

不过好像没有什么改变,如果我只是尝试登录的东西。例如:

However nothing seems to happen if i just try to log something. Example:

angular
    .module('app')
    .config(function($locationProvider) {
        $locationProvider.html5Mode(true);
    })
    .value('$routerRootComponent', 'app')
    .component('app', {
          templateUrl:'landing.html',
          controller: MainController,
          $routeConfig: [
                { path: '/', name: 'Dashboard', component: 'dashboard', useAsDefault: true },
                { path: '/media', name: 'Media', component: 'media'}
                ...
          ]
    });


function MainController(){
    this.$routerOnActivate = function(next, previous){
        console.log('activated', next, previous);
    };
}

同样的code 这一点。$ routerOnActivate 工作,如果我把它放在任何这是在routeConfig指定的组件。不过很明显,我不希望让每个组件相同的检查,而是解决全球一次。

The same code this.$routerOnActivate works if i put it in any of the Components which are specified in the routeConfig. However obviously I don't want to make the same check in every component, but rather solve it once globally.

什么是方法1.5?

推荐答案

这是我的意见的要求,粘贴

Paste from my comment as requested

什么执行您的网页上加载检查?这将在angular.run完美运行。

What about perform your check on the loading on the page ? This would run perfectly in an angular.run.

如果你想处理会话过期,您可以添加拦截所有请求,并观察401响应。

And if you want to handle session expiration, you can add an interceptor to all requests and watch for a 401 response.

这篇关于角$ 1.5 routerOnActivate对根路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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