【发布时间】:2017-01-20 15:35:09
【问题描述】:
如何在应用程序中的页面之间共享数据。任何人都可以在 nativescript 中介绍 localstorage 吗?
【问题讨论】:
-
页面上的数据是什么意思?你的意思是全局变量还是什么?
-
是的,我想通过应用访问数据。
标签: javascript xml nativescript
如何在应用程序中的页面之间共享数据。任何人都可以在 nativescript 中介绍 localstorage 吗?
【问题讨论】:
标签: javascript xml nativescript
您可以与 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:更改应用程序设置中的函数名称和文档链接
【讨论】:
您的问题可以通过多种方式阅读,因此很难给您一个好的答案,但我会尝试:
使用上下文创建导航条目
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-sqlite 和nativescript-couchbase
【讨论】:
shared preferences。任何具有根级别访问权限的应用程序都可以访问此数据。我对iOS没有做太多研究。您的许多用户可能会使用有根手机,并且像这样保存敏感数据绝对是一件轻而易举的事。不过请查看 nativescript-securestorage。这可用于存储敏感数据。
如果你安装了“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的作者。
【讨论】:
添加 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"
【讨论】:
如果您使用的是 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);
【讨论】: