<small id='VER8E'></small><noframes id='VER8E'>

<i id='VER8E'><tr id='VER8E'><dt id='VER8E'><q id='VER8E'><span id='VER8E'><b id='VER8E'><form id='VER8E'><ins id='VER8E'></ins><ul id='VER8E'></ul><sub id='VER8E'></sub></form><legend id='VER8E'></legend><bdo id='VER8E'><pre id='VER8E'><center id='VER8E'></center></pre></bdo></b><th id='VER8E'></th></span></q></dt></tr></i><div id='VER8E'><tfoot id='VER8E'></tfoot><dl id='VER8E'><fieldset id='VER8E'></fieldset></dl></div>
    • <bdo id='VER8E'></bdo><ul id='VER8E'></ul>
    <legend id='VER8E'><style id='VER8E'><dir id='VER8E'><q id='VER8E'></q></dir></style></legend>
    1. <tfoot id='VER8E'></tfoot>

      1. Angular ui路由器 - 重定向根本不起作用

        时间:2023-09-30
          <bdo id='5DvtJ'></bdo><ul id='5DvtJ'></ul>

                <small id='5DvtJ'></small><noframes id='5DvtJ'>

                  <tbody id='5DvtJ'></tbody>

              1. <i id='5DvtJ'><tr id='5DvtJ'><dt id='5DvtJ'><q id='5DvtJ'><span id='5DvtJ'><b id='5DvtJ'><form id='5DvtJ'><ins id='5DvtJ'></ins><ul id='5DvtJ'></ul><sub id='5DvtJ'></sub></form><legend id='5DvtJ'></legend><bdo id='5DvtJ'><pre id='5DvtJ'><center id='5DvtJ'></center></pre></bdo></b><th id='5DvtJ'></th></span></q></dt></tr></i><div id='5DvtJ'><tfoot id='5DvtJ'></tfoot><dl id='5DvtJ'><fieldset id='5DvtJ'></fieldset></dl></div>

                  <tfoot id='5DvtJ'></tfoot>
                1. <legend id='5DvtJ'><style id='5DvtJ'><dir id='5DvtJ'><q id='5DvtJ'></q></dir></style></legend>
                  本文介绍了Angular ui路由器 - 重定向根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 ui.router 在我的 Angular 应用程序中进行路由.有一个典型的登录场景,如果用户没有登录,我会将他重定向到登录 url.没有 ui.router 库,使用 $routeChangeStart 事件结合 $location.path.现在,我正在使用 $stateChangeStart 事件,结合 $state.go,但没有任何效果!它还将我的浏览器发送到无限循环.我从其他来源得知这是一个已知错误,建议的解决方案都不适合我.此外,$location.path 也不会重定向到登录页面.

                  I'm using ui.router for routing in my Angular app. There is a typical login scenario, where I'm redirecting a user to the login url if he's not logged in. Without the ui.router library, this worked fine using $routeChangeStart event combined with $location.path. Now, I'm using the $stateChangeStart event, combined with $state.go, and the nothing works! It also sends my browser into an infinite loop. I read from other sources that this is a known bug, and none of the suggested solutions work for me. Moreover, $location.path too doesn't redirect to the login page.

                  这就是我配置路径的方式:

                  This is how I've configured my paths:

                   .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
                      $stateProvider
                          .state('main', {
                              url: '/',
                              templateUrl: 'views/main.html',
                              controller: 'MainCtrl'
                          })
                          .state('about', {
                              url: '/about',
                              templateUrl: 'views/about.html',
                              controller: 'AboutCtrl'
                          })
                          .state('login', {
                              url: '/login',
                              templateUrl: 'views/loginform.html',
                              controller: 'LoginCtrl'
                          });
                      $urlRouterProvider.otherwise("/");
                  }])
                  

                  还有我的 run 方法:

                  .run(['$state', '$rootScope', '$location', function($state, $rootScope, $location) {
                      //Check when routing starts
                      //event, next, current
                      $rootScope.$on( '$stateChangeStart', function(e, toState, toParams, fromState, fromParams) {
                          //Redirect to login if the user is not logged in
                          if (!isUserLoggedIn) {
                  
                              //Some suggestion
                              //e.preventDefault();
                              console.log('Not logged in');
                  
                              //Infinite loop, kills my browser!
                              $state.go('login');
                              $state.transitionTo('login');
                  
                              //Some suggestion
                              $state.go('login', { url: '/login'});
                  
                              //Doesn't work
                              $location.path('/login');
                  
                              //$location.path( $state.href('login');
                              console.log('Redirected');
                          }
                      });
                  

                  推荐答案

                  如果状态 TO 已经是登录",我们在这里需要的是不要重定向.

                  What we would need here, is to do NOT redirect, if the state TO is already the 'login'.

                  只是草拟的调整

                  $rootScope.$on( '$stateChangeStart', function(e, toState  , toParams
                                                                 , fromState, fromParams) {
                  
                      var isLogin = toState.name === "login";
                  
                      if(isLogin){
                  
                         return; // no need to redirect anymore 
                      }
                      ...
                  

                  而且,我们应该使用 event.preventDefault();

                   ...
                   // also prevent default on redirect
                   $state.go('login');
                   event.preventDefault(); 
                   ...
                  

                  请查看此问答A 如何修复超出最大调用堆栈大小"AngularJS和 a plunker 具有类似的解决方案

                  Please check this Q & A How can I fix 'Maximum call stack size exceeded' AngularJS And a plunker with similiar solution

                  这篇关于Angular ui路由器 - 重定向根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何实现“ui-sref"?有条件地执行? 下一篇:AngularJS - 启用没有 404 错误的 HTML5 模式页面刷新

                  相关文章

                    <i id='frLkA'><tr id='frLkA'><dt id='frLkA'><q id='frLkA'><span id='frLkA'><b id='frLkA'><form id='frLkA'><ins id='frLkA'></ins><ul id='frLkA'></ul><sub id='frLkA'></sub></form><legend id='frLkA'></legend><bdo id='frLkA'><pre id='frLkA'><center id='frLkA'></center></pre></bdo></b><th id='frLkA'></th></span></q></dt></tr></i><div id='frLkA'><tfoot id='frLkA'></tfoot><dl id='frLkA'><fieldset id='frLkA'></fieldset></dl></div>

                  1. <tfoot id='frLkA'></tfoot>
                    • <bdo id='frLkA'></bdo><ul id='frLkA'></ul>

                      <legend id='frLkA'><style id='frLkA'><dir id='frLkA'><q id='frLkA'></q></dir></style></legend>

                      <small id='frLkA'></small><noframes id='frLkA'>