Defer Angular UI Router $stateChangeStart until server authorization response receieved

前端 未结 4 440
攒了一身酷
攒了一身酷 2020-12-16 15:19

I have an Angular app using UI Router where I\'m trying to validate a user\'s token, if one exists, when the app runs. I am also checking that the user has permission to acc

4条回答
  •  春和景丽
    2020-12-16 16:01

    I guess it's too late for answering this question, but I want to comment here for someone who may need a little idea.

    I've tried to do almost same thing for a long time and I stopped doing that. I don't think stateChangeStart listener is reliable to use. I think the best way to do your what you want is decorating $state.go using $provide service. Here is an example:

    /* config your angular app */
    .config(function($provide) {
        $provide.decorator('$state', function($delegate) {
    
            var myState = $delegate;
            // Rename original 'go' method
            myState.originalGo = myState.go;
    
            myState.go = function(toState, toParams, options) {
    
            /*  
             *  Do what you want to do :)
             */
    
            this.originalGo(toState, toParams, options);
        };
    
        return $delegate;
      });
    

    With this approach, you can do whatever you want before $stateChangeStart. I hope it helps.

提交回复
热议问题