在AppGyver中使用AppVars来存储和读取应用程序变量和状态,语法如下:
Set App Variable:
setItem(key: string, value: any, persistence: any)
例子:
AppVars.setItem("myKey", "myValue", "app");
Get App Variable:
getItem(key: string, persistence: any)
例子:
const myValue = AppVars.getItem("myKey", "app");
2. 对于Android设备,您还可以使用共享首选项来存储和读取应用程序数据。首先,您需要在模块配置中添加“Shared Preferences”模块(https://docs.appgyver.com/reference/data/storage/shared-preferences)。然后,您可以使用以下代码示例:
const PREFS_NAME = 'my_prefs'; // 定义首选项名称 const prefs = AppGyverNativeBridge.sharedPreferences(PREFS_NAME);
// 存储数据 prefs.putString('myKey', 'myValue');
// 读取数据 const myValue = prefs.getString('myKey');
注意:Shared Preferences存储的数据是应用程序特定的,只能由您的应用程序访问。如果您需要将数据在多个应用程序之间共享,则需要使用其他存储机制,比如SQLite数据库。
最后,为了确保数据安全,建议将敏感数据加密后存储。