【问题标题】:can't logout from ionic无法从离子注销
【发布时间】:2015-05-29 02:12:01
【问题描述】:

您好,我遇到了 ionic 登录和注销问题。

每次注销后,我仍然可以单击返回按钮,它会带我回到上一页。我可以知道如何在注销时清除或删除会话,以便用户无法从登录返回上一页吗?

var default_stat;
$scope.logout = function(){
    $ionicLoading.show({template:'Logging out....'});
    $localstorage.set('loggin_state', '');
    $state.go('login');
    $ionicLoading.hide();
    $ionicHistory.clearHistory();
    $ionicHistory.clearCache();
};

在登录过程中,我使用 localstorage 来指示用户已登录

$localstorage.set('loggin_state', '1');

【问题讨论】:

  • 我有同样的问题,它没有清除缓存或历史记录。我在文档中读到 $ionicHistory 只会清除所有其他视图的缓存,除了您当前所在的视图。我需要回答同样的问题。

标签: angularjs ionic-framework angular-local-storage


【解决方案1】:

我会这样做:

$scope.logout = function(){
    $ionicLoading.show({template:'Logging out....'});
    $localstorage.set('loggin_state', '');

    $timeout(function () {
        $ionicLoading.hide();
        $ionicHistory.clearCache();
        $ionicHistory.clearHistory();
        $ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true });
        $state.go('login');
        }, 30);

};

我发现添加一点延迟可以让$ionicHistory 清除缓存。

$ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true });
  • disableBack:下一个视图应该忘记它的后视图,并将其设置为 null。
  • historyRoot:下一个视图应该成为其历史堆栈中的根视图。

【讨论】:

    【解决方案2】:

    $ionicHistory.clearCache() 现在返回承诺,因此您可以确保清除缓存。所以你可以这样调用:

    $ionicHistory.clearCache().then(function() {
        //now you can clear history or goto another state if you need
        $ionicHistory.clearHistory();
        $ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true });
        $state.go('login');
    })
    

    上面的超时之类的东西就没有必要了。

    credit goes to

    【讨论】:

      【解决方案3】:

      这是因为ionic 正在缓存视图,所以这将阻止 ionic 通过控制器。

      所以你可以按如下方式破坏缓存

      <ion-view cache-view="false" view-title="My Title!">
        ...
      </ion-view> 
      

      read here for more

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-12
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-19
        • 2019-04-24
        相关资源
        最近更新 更多