【问题标题】:Is there localstorage in nativescript?nativescript中有本地存储吗?
【发布时间】:2017-01-20 15:35:09
【问题描述】:

如何在应用程序中的页面之间共享数据。任何人都可以在 nativescript 中介绍 localstorage 吗?

【问题讨论】:

  • 页面上的数据是什么意思?你的意思是全局变量还是什么?
  • 是的,我想通过应用访问数据。

标签: javascript xml nativescript


【解决方案1】:

您可以与 global.foo 一起使用,它可用于整个应用程序,也可以使用应用程序设置模块

var applicationSettings = require("application-settings");
//set somewhere like this
applicationSettings.set<Number|String|Boolean>("sharedVar",1);

//get sharedVar somewhere
applicationSettings.get<Number|String|Boolean>("sharedVar");//if empty it will be null

//or if u want default value if sharedVar wasn't defined
//and if sharedVar was defined then u will get content of sharedVar
applicationSettings.get<Number|String|Boolean>("sharedVar","Default Value");

文档

https://docs.nativescript.org/cookbook/application-settings

https://docs.nativescript.org/api-reference/modules/_application_settings_.html

编辑:错字不是全局变量而是全局变量:D

EDIT2:更改应用程序设置中的函数名称和文档链接

【讨论】:

    【解决方案2】:

    您的问题可以通过多种方式阅读,因此很难给您一个好的答案,但我会尝试:

    如果您想通过导航将数据从一个页面传递到另一个页面

    使用上下文创建导航条目

    var navigationEntry = {
        moduleName: "details-page",
        context: {info: "something you want to pass to your page"},
        animated: false
    };
    topmost.navigate(navigationEntry);
    

    ...在您导航到的页面上,选择该上下文:

    function onLoaded(args) {
        console.log(args.object.navigationContext);
    }
    

    See documentation about Navigation

    如果您想创建整个应用程序中可用的数据

    只需创建一个 singleton 并请求它,就像在任何其他 Javascript 应用程序中一样。

    例如

    文件:myData.js

    var data = {
        something: 'a value here',
        somethingElse: 1
        somethingMany: ['a', 'b', 'c']
    };
    
    exports.data = data;
    

    在您要读取该数据的任何文件中:

    var data = require("./myData.js").data;
    console.log(data);
    

    Read more about modules in Javascript

    如果你想在本地设备上持久化数据

    如果你想写入和读取数据,以便在会话之间保存:

    对于非复杂数据,请使用application-settings。例如

    var appSettings = require("application-settings");
    
    appSettings.setString("stringKey", "String value");  // Writing
    var value = appSettings.getString("stringKey", "No string value"); // Reading
    // will return "No string value" if there is no value for "stringKey"
    
    console.log(value)
    

    Read the docs about application-settings

    您也可以使用file-system 模块向设备写入文件,例如

    var documents = fs.knownFolders.documents();
    var path = fs.path.join(documents.path, "FileFromPath.txt");
    var file = fs.File.fromPath(path);
    
    // Writing text to the file.
    file.writeText("Something")
        .then(function () {
            // Succeeded writing to the file.
        }, function (error) {
            // Failed to write to the file.
        });
    

    阅读有关 file-system 的文档

    对于数据库,您可以使用一些模块,例如nativescript-sqlitenativescript-couchbase

    【讨论】:

    • “如果你想在本地设备上持久化数据”用这种方法存储敏感数据是一个好习惯吗?
    • @JDrake 此数据可以被其他应用程序读取,不应用于存储敏感数据
    • @itaintme 你能详细说明一下吗?其他应用程序如何读取数据?在哪些平台上?
    • @JDrake 这在 android 上使用未加密的 shared preferences。任何具有根级别访问权限的应用程序都可以访问此数据。我对iOS没有做太多研究。您的许多用户可能会使用有根手机,并且像这样保存敏感数据绝对是一件轻而易举的事。不过请查看 nativescript-securestorage。这可用于存储敏感数据。
    • 但是什么应用有root权限?应用程序具有root访问权限不是已经很糟糕了吗?这不会让上述应用程序做更糟糕的事情吗? (我真的很想知道,不涉及讽刺)
    【解决方案3】:

    如果你安装了“nativescript-localstorage”插件,

    tns plugin install nativescript-localstorage

    然后您将可以访问 SessionStorage 和 LocalStorage,就像您使用浏览器一样。


    来自docs

    设置值:

    require( "nativescript-localstorage" );
    localStorage.setItem('Another Plugin', 'By Master Technology');
    localStorage.getItem('Another Plugin'); // Returns: "By Master Technology"
    

    const LS = require( "nativescript-localstorage" );
    LS.setItem('Another Plugin', 'By Master Technology');
    LS.getItem('Another Plugin');  // Returns: "By Master Technology"
    

    免责声明我是plugin的作者。

    【讨论】:

    • 我们如何存储对象?
    • 在 nativescript 6 版本中已弃用...移至付费仓库
    • 小说明:community 版本仍可按原样使用。 ProPlugins.org 中提供了新的增强功能和错误修复
    • 这个插件可以在 Nativescript 7 iOS 中使用吗?我在 iOS 中遇到错误
    • 社区版本应该还在工作,我不知道它应该有什么问题,但我不再维护它。我仍在维护 ProPlugins 版本,并且它确实可以工作,因为我在多个应用程序中使用它。
    【解决方案4】:

    添加 LocalStorage 和 SessionStorage 的 NativeScript 插件 如果您尝试使用任何使用 localStorage/sessionStorage API 的库;或者你想要一个相当简单的存储引擎;在这里。

    检查nativescript-localstorage模块

    用法

    要使用该模块你只需require()它:

    require( "nativescript-localstorage" );
    

    或者您也可以在您的 app.module 或文件中导入 localstorage 模块。

    import 'nativescript-localstorage';
    

    然后在您的代码中使用localStorage 变量,如下所示。

    localStorage.setItem('Another Plugin', 'By Master Technology');
    

    这将启用 localStorage api。这样你就可以像浏览器一样使用它了。

    您还可以选择执行以下操作:

    let LS = require( "nativescript-localstorage" );
    LS.getItem('Another Plugin'); // Returns: "By Master Technology"
    

    【讨论】:

      【解决方案5】:

      如果您使用的是 NS8,请使用来自 @nativescript/core 的“应用程序设置”

      例如:

      import * as appSettings from "@nativescript/core/application-settings";
      ...
      appSettings.setString("token", response.token);
      appSettings.setString("userId", response.user.id.toString());
      appSettings.setString("email", email);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-17
        • 2019-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-04
        • 1970-01-01
        • 2021-05-03
        相关资源
        最近更新 更多