javascript AngularJS 仅在浏览器的后退按钮上重定向路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19686169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:29:05  来源:igfitidea点击:

AngularJS redirect a route only on browser's back button

javascriptangularjsredirectbackangularjs-routing

提问by Atropo

In my AngularJS application I'm redirecting the routeto a specific page when the user isn't logged. To do that I'm using a variable on $rootScope.

在我的 AngularJS 应用程序中,route当用户未登录时,我将其重定向到特定页面。为此,我在$rootScope.

Now I would like to prevent the browser's back button when the user is logged. I would like to redirect it to a specific page (the registrationview). The problem is I don't know if there's a back button event.

现在我想在用户登录时阻止浏览器的后退按钮。我想将其重定向到特定页面(registration视图)。问题是我不知道是否有后退按钮事件

My code is:

我的代码是:

 angular.module('myApp',[...]
//Route configurations
}])
.run(function($rootScope, $location){
               $rootScope.$on('$routeChangeStart', function(event, next, current){
                   if(!$rootScope.loggedUser) { 
                       $location.path('/register');
                   }
               });
               $rootScope.$on('$locationChangeStart', function(event, next, current){
                   console.log("Current: " + current);
                   console.log("Next: " + next);
               });
           });

So on $locationChangeStartI would write a pseudocode like:

所以$locationChangeStart我会写一个伪代码,如:

if (event == backButton){
     $location.path('/register');
}

Is it possible?

是否可以?

A naive solution would be writing a function that checks if nextand currentare in the wrong order, detecting if the user is going back.

一个简单的解决方案是编写一个函数来检查nextcurrent的顺序是否错误,检测用户是否要返回

There are other solutions? I'm approaching the problem in a wrong way?

还有其他解决方案吗?我以错误的方式解决问题?

回答by Atropo

I found a solution, which is easier than I thought. I register on a object in $rootScopethe actual location and on every location change I check with the new one. In this way I can detect if the user is going back in the history.

我找到了一个解决方案,这比我想象的要容易。我在$rootScope实际位置的对象上注册,并在每次位置更改时检查新的对象。通过这种方式,我可以检测用户是否正在返回历史记录。

angular.module('myApp',[...], {
    //Route configurations
}])
.run(function($rootScope, $location) {
    $rootScope.$on('$routeChangeStart', function(event, next, current) {
        if(!$rootScope.loggedUser) { 
            $location.path('/register');
        }
    });

    $rootScope.$on('$locationChangeSuccess', function() {
        $rootScope.actualLocation = $location.path();
    });

    $rootScope.$watch(function() { return $location.path() },
        function(newLocation, oldLocation) {
            if($rootScope.actualLocation == newLocation) {
                $location.path('/register');
            }
        }); 
    });
});