如何将url传递给ui-router的$ stateChangeStart

时间:2020-12-30 19:37:23

I use the following to redirect routes to login if a user is not logged in:

如果用户未登录,我使用以下内容将路由重定向到登录:

angular.module('app').run(['$rootScope', '$location', '$state', 'AuthService', 
    function($rootScope, $location, $state, AuthService) {

    /* redirect to login if not logged in */
    $rootScope.$on( '$stateChangeStart', 
        function(e, toState, toParams, fromState, fromParams) {
            if(toState.name === "login"){
               return; // already going to login 
            }

            if(!AuthService.user) {
                e.preventDefault(); 
                $state.go('login'); 
            }
        });
}]);

How can I pass the url from the "prevented" state to the login state so that I can continue navigating to that state once logged in?

如何将URL从“已阻止”状态传递到登录状态,以便我可以在登录后继续导航到该状态?

Example: User clicks link for myapp.com/#/path/to/data --> redirects to myapp.com/#/login --> user logs in --> user routed to myapp.com/#/path/to/data

示例:用户单击myapp.com/#/path/to/data链接 - >重定向到myapp.com/#/login - >用户登录 - >用户路由到myapp.com/#/path/to /数据

I basically need /path/to/data to be sent to my login controller so that I can handle that. I cannot seem to find '/path/to/data' in any of the state or param variables in the $stateChangeStart listener.

我基本上需要将/ path / to / data发送到我的登录控制器,以便我可以处理它。我似乎无法在$ stateChangeStart监听器中的任何状态或参数变量中找到'/ path / to / data'。

2 个解决方案

#1


3  

The $state service has an href method which can create a URL given the state and its parameters. Since those are provided to the state change listener, you can create a URL and then pass it as a param to the login state.

$ state服务有一个href方法,可以在给定状态及其参数的情况下创建URL。由于这些是提供给状态更改侦听器的,因此您可以创建一个URL,然后将其作为参数传递给登录状态。

$rootScope.$on('$stateChangeStart',
        function(e, toState, toParams, fromState, fromParams) {
            if (toState.name === "login") {
                return; // already going to login
            }

            if (!AuthService.user) {
                e.preventDefault();

                var redirectUrl = $state.href(toState.name, toParams);

                $state.go('login', {
                    redirect: redirectUrl
                });
            }
        });

You'll need to add a redirect parameter to your login state so you can use that.

您需要在登录状态中添加重定向参数,以便可以使用它。

#2


-2  

You can store the pre-login location in $rootScope, and then fetch it from there once login is successful

您可以将登录前位置存储在$ rootScope中,然后在登录成功后从那里获取它

if(!AuthService.user) {
    e.preventDefault(); 
    $rootScope.pre_login_path = $location.path();
    $state.go('login'); 
}

And then after login, you can set the login path similarly in your login controller/service (have that include dependency injection of $rootScope and $location)

然后在登录后,您可以在登录控制器/服务中类似地设置登录路径(包括$ rootScope和$ location的依赖注入)

$location.path($rootScope.pre_login_path);

#1


3  

The $state service has an href method which can create a URL given the state and its parameters. Since those are provided to the state change listener, you can create a URL and then pass it as a param to the login state.

$ state服务有一个href方法,可以在给定状态及其参数的情况下创建URL。由于这些是提供给状态更改侦听器的,因此您可以创建一个URL,然后将其作为参数传递给登录状态。

$rootScope.$on('$stateChangeStart',
        function(e, toState, toParams, fromState, fromParams) {
            if (toState.name === "login") {
                return; // already going to login
            }

            if (!AuthService.user) {
                e.preventDefault();

                var redirectUrl = $state.href(toState.name, toParams);

                $state.go('login', {
                    redirect: redirectUrl
                });
            }
        });

You'll need to add a redirect parameter to your login state so you can use that.

您需要在登录状态中添加重定向参数,以便可以使用它。

#2


-2  

You can store the pre-login location in $rootScope, and then fetch it from there once login is successful

您可以将登录前位置存储在$ rootScope中,然后在登录成功后从那里获取它

if(!AuthService.user) {
    e.preventDefault(); 
    $rootScope.pre_login_path = $location.path();
    $state.go('login'); 
}

And then after login, you can set the login path similarly in your login controller/service (have that include dependency injection of $rootScope and $location)

然后在登录后,您可以在登录控制器/服务中类似地设置登录路径(包括$ rootScope和$ location的依赖注入)

$location.path($rootScope.pre_login_path);