您的问题可以通过多种方式来解读,这使得给您一个好的答案有点困难,但我会尝试:
如果您想在导航上将数据从一个页面传递到另一个页面
创建带有上下文的导航条目
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);
}
请参阅有关导航的文档 https://docs.nativescript.org/core-concepts/navigation#navigate-and-pass-context
如果您想创建整个应用程序可用的数据
只需创建一个单例 https://en.wikipedia.org/wiki/Singleton_pattern并请求它,就像在任何其他 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);
阅读有关 Javascript 模块的更多信息 https://medium.freecodecamp.com/javascript-modules-a-beginner-s-guide-783f7d7a5fcc#.vtthlzon8
如果您想将数据持久保存在本地设备上
如果您想写入和读取数据,以便可以在会话之间保存它:
对于非复杂数据, use 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)
阅读有关应用程序设置的文档 https://docs.nativescript.org/cookbook/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.
});
阅读有关的文档文件系统 https://docs.nativescript.org/cookbook/file-system
对于数据库您可以使用一些模块,例如原生脚本sqlite https://www.npmjs.com/package/nativescript-sqlite and NativeScript-couchbase https://github.com/couchbaselabs/nativescript-couchbase