ui-router:重置$ state而不使用$ state.go()

时间:2016-01-25 23:50:35

标签: javascript angularjs angular-ui-router state reload

首先,我认为这里的整体问题是AngularJS仍然没有一种明智的,最好的实践方式来重新启动"应用程序及其所有组件。到目前为止,最好的做法似乎是设置一个"默认"查看,然后重新加载窗口。我们在/ logout状态下执行此操作,如下所示:

$stateProvider.state('logout', {
    onEnter: function($state, $window, store) {
        //Remove any session variables
        store.remove('varA');
        store.remove('varB');

        //"Logout" the user
        $state.go('login');
        $window.location.reload();
    }
})

但是,这是一种糟糕的用户体验。在窗口重新加载之前,用户实际上可以看到视图更改和页面上的组件。整体上看起来非常错误。

最初,我们没有包含$state.go('login'),并且没有获得在注销前看到视图更改的奇怪体验。但是,当我们开始使用像$rootScope.$on('$stateChangeStart', function (event, toState, toParams)这样的$状态处理程序时,我们注意到窗口重新加载后toState没有被重置。因此,$stateChangeStart将在重新加载后触发,并且toState仍将设置为用户在调用/logout之前所处的任何视图。这不是一个真正的应用程序重置!

可能的解决方案:我认为如果有办法重置$ state"所有这一切都可以解决。不使用$state.go()方法......而是在幕后发生的事情。

1 个答案:

答案 0 :(得分:1)

简单的代码更改为我修复了

//"Logout" the user
$state.go('login', null, {notify: false}).then(function() {
    $window.location.reload();
});

来源:https://github.com/angular-ui/ui-router/issues/2486#issuecomment-180872463