【问题标题】:ui-router: Reset $state without using $state.go()ui-router:在不使用 $state.go() 的情况下重置 $state
【发布时间】:2016-05-02 11:23:14
【问题描述】:

首先,我认为这里的整体问题是 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) 这样的$state 处理程序时,我们注意到toState 在窗口重新加载后没有被重置。因此$stateChangeStart 将在重新加载后触发,toState 仍将设置为用户在调用/logout 之前所在的任何视图。这不是真正的应用重置!

可能的解决方案:我认为如果有一种方法可以在不使用$state.go() 方法的情况下“重置$state”,那么所有这些都可以解决……而是在幕后发生的事情。

【问题讨论】:

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


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多