Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS UI路由器在重新路由过程中闪烁_Javascript_Angularjs_Angular Ui Router - Fatal编程技术网

Javascript AngularJS UI路由器在重新路由过程中闪烁

Javascript AngularJS UI路由器在重新路由过程中闪烁,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,我有AngularJS ui路由器,正在检查状态更改期间的身份验证状态。在这个变化过程中,当AngularJS决定是否应该显示该页面时,我经历了一个闪烁。例如,如果用户已登录,/#/将受到保护并重定向到/#/home。然而,我看到了/#/(的html的一个简短的一瞥,然后重定向发生了。我怎样才能阻止这种闪烁的发生 function authenticate($q, $state, $timeout, AuthService) { AuthService.isLoggedIn()

我有AngularJS ui路由器,正在检查状态更改期间的身份验证状态。在这个变化过程中,当AngularJS决定是否应该显示该页面时,我经历了一个闪烁。例如,如果用户已登录,/#/将受到保护并重定向到/#/home。然而,我看到了/#/(的html的一个简短的一瞥,然后重定向发生了。我怎样才能阻止这种闪烁的发生

    function authenticate($q, $state, $timeout, AuthService) {
    AuthService.isLoggedIn()
        .then(function () {
            // Resolve the promise successfully
            console.log("auth")
            return $q.when()
        })
        .catch(function () {

            $timeout(function () {
                // This code runs after the authentication promise has been rejected.
                // Go to the log-in page
                $state.go('main')
            })

            // Reject the authentication promise to prevent the state from loading
            return $q.reject()
        })


}

function notAuthenticate($q, $state, $timeout, AuthService) {
    AuthService.shouldNotBeLoggedIn()
        .then(function () {
            // Resolve the promise successfully
            console.log("not auth")
            return $q.when()

        }).catch(function () {

            $timeout(function () {
                // This code runs after the authentication promise has been rejected.
                // Go to the log-in page
                $state.go('home')
            })
            // Reject the authentication promise to prevent the state from loading
            return $q.reject()

        })

}

/** @ngInject */
function routeConfig($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'app/home/home.html',
            controller: 'HomeController',
            controllerAs: 'home',
            resolve: {authenticate: authenticate}
        })

        .state('main', {
            url: '/',
            templateUrl: 'app/main/main.html',
            controller: 'MainController',
            controllerAs: 'main',
            resolve: {notAuthenticate: notAuthenticate}
        })

状态的
resolve
用于在激活状态之前“解析”一些可注入项。因此,“resolves”(
resolve:{…}
)的属性需要一个返回值或承诺值的函数。在进行身份验证时,可能是
用户

在您的情况下,解析函数不会返回承诺-它们只是立即运行并完成,从而激活状态。然后,一段(短)时间后,您的
AuthService
启动并决定更改状态。这会导致闪烁

function authenticate($q, $state, $timeout, AuthService) {

   // this "return" is what returns the promise
   return AuthService.isLoggedIn()
                     .then(....);
}

您的“resolves”立即解析,因为
authenticate
notAuthenticate
函数都不会返回承诺。因此,在
main
状态的情况下,您成功地进入该状态,但不久之后被重定向到
home
@NewDev似乎
$timeout
是问题所在,它从
登录到页面。catch
函数&重定向到下一个摘要中..我不确定..@NewDev感谢NewDev。您的答案是正确的。如果你贴出来,我会给你这个问题。