Angular ui-路由器 $state.go 没有在 resolve 内重定向

Angular ui-router $state.go is not redirecting inside resolve

在我的 angularjs 应用中,我正在检查用户是否登陆登陆页面并且已经通过身份验证,然后将他重定向到主页。

.state('landingpage', {
            abstract: "true",
            url: "/landingpage",
            templateUrl: "app/landingpage/landingpage.html",
            resolve: {
                AutoLoginCheck: ['$state', '$window', function ($state, $window) {

                    if($window.localStorage.access_token != null)
                    {
                        if($window.sessionStorage.access_token == null) {
                            $window.sessionStorage.access_token = $window.localStorage.access_token;
                        }
                        UserInfoService.SetUserAuthenticated(true);

                        // it is not redirecting
                        return $state.go('app.home');

                    }
                }]
            }
        })

问题是尽管所有解析代码都成功 运行,但用户没有被重定向到 app.home。谁能告诉我为什么会这样?

注意:状态'app'也有一个解析,在'app.home'状态'app.home'获取要显示的数据。

您的问题可能有两种解决方案

  • 首先,您可以发出一个事件,侦听器将处理您的状态转换。您可以在父控制器的任何地方实现侦听器

  • 其次,您可以实现 $stateChangeStart 挂钩并在那里检查您的重定向条件

    $rootScope.$on('$stateChangeStart', function (event, toState) {      
         if (toState.name === 'landingpage') {              
           if (!isAuthenticated()) { // Check if user allowed to transition                  
                event.preventDefault();   // Prevent migration to default state                  
                $state.go('home.dashboard');           
            }
          }
    });
    

您可以使用 resolve 为您的控制器提供状态自定义的内容或数据。 resolve 是一个可选的依赖映射,它应该被注入到控制器中。

您可以有一个控制器来检查 AuthState 并相应地进行重定向。

     .state('landingpage', {
        abstract: "true",
        url: "/landingpage",
        templateUrl: "app/landingpage/landingpage.html",
        resolve: {
            AutoLoginCheck: ['$window', function ($window) {

                if($window.localStorage.access_token != null)
                {
                    if($window.sessionStorage.access_token == null) {
                        $window.sessionStorage.access_token = $window.localStorage.access_token;
                    }

                   //assuming userInfoService does the authentication
                    var isAuthenticated = userInfoService.SetUserAuthenticated(true);
                    return isAuthenticated;

                }
            }]
        },
        controller: ['$state','AutoLoginCheck', function($state, AutoLoginCheck){
          if(AutoLoginCheck){
            //authenticated
            $state.go('app.home');
          } else {
            //redirect to unauthenticated page
            $state.go('....');
          }
        }]
    })
.state('landingpage', {
            abstract: "true",
            url: "/landingpage",
            templateUrl: "app/landingpage/landingpage.html",
            resolve: {
                AutoLoginCheck: ['$state','$window', '$q','$timeout', function ($state, $window,$q,$timeout) {

                    if($window.localStorage.access_token != null)
                    {
                        if($window.sessionStorage.access_token == null) {
                            $window.sessionStorage.access_token = $window.localStorage.access_token;
                        }
                        UserInfoService.SetUserAuthenticated(true);


                        $timeout(function() {
                           $state.go('app.home')
                        },0);
                        return $q.reject()

                    }
                }]
            }
        })

这对你有用。

您可以改用 $location.url('/')

这是一个旧线程,但我使用 $location.path() 在 state.resolve() 块

内完成重定向

无论如何解决等待承诺状态。你能做的最好的事情是 return 承诺并为你的状态添加超时:

resolve: {
    AutoLoginCheck: ['$state', '$window', '$timeout', '$q', function ($state, $window, $timeout, $q) {
        var deferred = $q.defer();
        if(user.isLogin()){
             deferred.resolve();
        }else{
          $timeout(function(){
            $state.go('app.home');
          }
          deferred.reject();
        }
        return deferred.promise;
    }]