是否可以清除 Ionic 中的视图缓存? [英] Is it possible to clear the view cache in Ionic?

查看:28
本文介绍了是否可以清除 Ionic 中的视图缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个 Angular/Ionic/Cordova 项目,我们最近升级到了最新的 Ionic 测试版.从项目之前使用的版本开始,这引入了视图缓存.然而,这样做也带来了一个问题.

I'm currently working on a Angular/Ionic/Cordova project and we've recently upgraded to the latest Ionic beta. From the version the project was using before, this introduced the view cache. It has also however introduced an issue in doing so.

该应用程序面向客户并且非常以数据为中心.但是,目前用户必须进行身份验证才能查看与其帐户关联的数据;当用户注销并登录另一个帐户时,因为视图仍处于缓存状态,所以会显示上一个帐户的视图.

The application is customer-facing and is very data-centric. A user must authenticate to view data associated with their account, currently however; when a user logs out, and logs into another account, because the view(s) are still cached they are presented with the views of the last account.

当用户登录时,应用程序仍应缓存视图,因为它有助于使应用程序感觉更快,但应在用户注销时清除缓存.

The app should still cache the views when the user is logged in, as it helps make the app feel much quicker, but the cache should be purged when a user logs out.

设置 cache-view="false" 不是一个选项,因为它会完全禁用缓存.

Setting cache-view="false" is not an option, as it would completely disable the cache.

我也试过设置 $ionicConfig.views.maxCache(0); 然后回到默认值 10,希望这样做可以清除缓存,但它有没有效果.

I've also tried setting $ionicConfig.views.maxCache(0); and then back to the default of 10 in the hopes that it would purge the cache in doing so, but it had no effect.

我能想到的最后一件事是在用户登录时触发一个事件,该事件刷新当前加载到视图中的所有数据 - 但是,这将比我认为应该花费更多的努力.

The last thing I can think of to do is fire an event when a user logs in that refreshes all data that's currently loaded into the views - however, this would take a bit more effort than I feel it should.

有没有办法简单地清除视图缓存?

Is there a way to simply clear the view cache?

推荐答案

在 app.js 中的状态定义中,您可以添加 cache:false 以禁用缓存(参见 在 Ionic 文档中禁用状态提供程序中的缓存.或者,您可以继续缓存,除非您知道数据已更改.

In the state definition in app.js you can add cache:false to disable caching (See Disable cache within state provider in the Ionic docs. Or, you can keep caching except when you know data has changed.

  • 如果你已经在页面上,你可以这样做,$state.go($state.currentState, {}, {reload:true})
  • 如果您处于另一个状态并且想要返回到通常缓存的状态但您希望它被刷新,您可以这样做,$ionicHistory.clearCache().then(function(){$state.go('app.fooState') })

注意,后者需要 clearCache 来返回一个 promise.查看我在此拉取请求中所做的更改,并将您现在拥有的 clearCache 实现与我的进行比较:https://github.com/driftyco/ionic/pull/3724

Note, the latter requires clearCache to return a promise. See the changes I made in this pull request and compare the implementation of clearCache that you have now with mine: https://github.com/driftyco/ionic/pull/3724

这篇关于是否可以清除 Ionic 中的视图缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆