离子本地存储与使用服务 [英] Ionic local storage vs using service

查看:25
本文介绍了离子本地存储与使用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己将来自 api 调用的所有数据都放在设备本地存储中,我不确定是将东西放在本地存储中还是将它们放在服务中是否重要.什么时候应该使用设备的本地存储与使用 Angularjs 服务?

I am finding myself placing all my data from my api calls in the device local storage and I am not sure if it matters whether I put things in local storage vs putting them in a service. When should I use local storage from device vs using a Angularjs service?

推荐答案

你有点混淆概念.说到 angularjs 服务,只要页面没有刷新,它们就会持久化数据,只要您刷新页面或关闭浏览器选项卡,数据就消失了.

You are a bit mingled up with concepts. When it comes to angularjs services, they persist data as long as the page is not refreshed, as soon as you refresh your page or close the browser tab, The data's gone.

将 Angularjs 服务视为您声明的纯粹变量,其范围限定在浏览器选项卡的生命周期内.因此,您可以使用它来存储一些不打算传递到下一个会话的临时标志和值.

Consider Angularjs services as mere variables that you declare, which are scoped to the lifetime of your browser tab. Hence, you can use it to store some temporary flags and values that aren't meant to be carried forward to next session.

然而,当谈到 localStorage 时,请将其视为一种数据库类型的东西.您在 localStorage 中存储的任何内容都会保存在浏览器中,并且可以在多个标签页和应用会话中使用 [直到和除非用户清除浏览器数据].

Whereas, When it comes to localStorage, consider it as a database kind of stuff. Whatever you store in localStorage, is saved inside the browser, and will be available across multiple tabs, and sessions of your apps [Until and unless user clears browser data].

由于您使用的是 Ionic 和 Cordova,因此您必须使用 localStorage 来保存用户名和密码等内容,以便用户下次打开您的应用时可以使用它们.请注意,关闭应用等同于关闭浏览器标签.

Since you're using Ionic and Cordova, you must use localStorage to save stuff such as user name and password, so that the user can use them the next time he opens your app. Take a note that, closing your app is equivalent to closing a browser tab.

然而,如果您的某些数据在用户每次访问您的应用时不断刷新,您可以使用服务来存储它们,以便在应用关闭后立即将其删除.

Whereas, if you have certain data that keeps refreshing each time user visits your app, you can use services to store them, so that they are removed as soon as the app's closed.

比喻,localStorage --> 二级、非易失性存储、angularjs 服务 --> 主要、易失性存储.

这篇关于离子本地存储与使用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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