如何在驻留在应用程序中的页面之间共享数据。任何人都可以在nativescript中讲述localstorage吗?
答案 0 :(得分:20)
你的问题可以通过各种方式阅读,让你有点难以给出一个好的答案,但我会尝试:
使用上下文创建导航条目
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应用中一样。
E.g。
file: 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
。 E.g。
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-sqlite和nativescript-couchbase
答案 1 :(得分: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");
DOCS :
https://docs.nativescript.org/cookbook/application-settings
https://docs.nativescript.org/api-reference/modules/_application_settings_.html
编辑:打字错误不是全局,而是全局:D
EDIT2 :更改applicationSettings中的函数名称和docs链接
答案 2 :(得分:6)
如果您安装“nativescript-localstorage”插件,
tns plugin install nativescript-localstorage
您将可以访问SessionStorage和LocalStorage,就像使用浏览器一样。
来自docs:
设置值:
require(“nativescript-localstorage”);
localStorage.setItem('Another Plugin','By Master Technology');
localStorage.getItem('Another Plugin'); //返回:“通过掌握技术”
或
const LS = require(“nativescript-localstorage”);
LS.setItem('Another Plugin','By Master Technology');
LS.getItem('Another Plugin'); //返回:“通过掌握技术”
免责声明我是plugin的作者。
答案 3 :(得分:1)
添加LocalStorage和SessionStorage的NativeScript插件如果您尝试使用任何使用localStorage / sessionStorage API的库;或者你想要一个相当简单的存储引擎;在这里。
<强>用法强>
要使用模块,只需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"
答案 4 :(得分:0)
如果您使用的是 NS8,请使用@nativescript/core 中的“application-settings”
例如:
import * as appSettings from "@nativescript/core/application-settings";
...
appSettings.setString("token", response.token);
appSettings.setString("userId", response.user.id.toString());
appSettings.setString("email", email);