nativescript中是否存在localstorage?

Nar*_*ren 8 javascript xml nativescript

如何在驻留在应用程序中的页面之间共享数据.任何人都可以在nativescript中讲述localstorage吗?

Emi*_*erg 22

你的问题可以通过各种方式阅读,让你有点难以给出一个好的答案,但我会尝试:

如果要在导航时将数据从一个页面传递到另一个页面

使用上下文创建导航条目

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

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

function onLoaded(args) {
    console.log(args.object.navigationContext);
}
Run Code Online (Sandbox Code Playgroud)

请参阅有关导航的文档

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

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

例如

file:myData.js

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

exports.data = data;
Run Code Online (Sandbox Code Playgroud)

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

var data = require("./myData.js").data;
console.log(data);
Run Code Online (Sandbox Code Playgroud)

在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)
Run Code Online (Sandbox Code Playgroud)

阅读有关应用程序设置的文档

您也可以使用模块将文件写入设备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.
    });
Run Code Online (Sandbox Code Playgroud)

阅读有关文件系统的文档

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


Mar*_*zay 8

您可以使用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");
Run Code Online (Sandbox Code Playgroud)

DOCS:

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

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

编辑:错字不是全局,而是全局:D

EDIT2:从applicationSettings更改函数名称和docs链接


Nat*_*ael 6

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

tns plugin install nativescript-localstorage

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


文档

设置值:

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

要么

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

免责声明我是上述插件的作者。

  • 在nativescript 6版本中已弃用...已移至付费存储库 (2认同)