中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
多个pageAbility之间如何共享变量
微信扫码分享
import dataPreferences from '@ohos.data.preferences'; import { BusinessError } from '@ohos.base'; let preferences: dataPreferences.Preferences | null = null; try { preferences.put('startup', 'auto', (err: BusinessError) => { if (err) { console.error(`Failed to put data. Code:${err.code}, message:${err.message}`); return; } console.info('Succeeded in putting data.'); }) } catch (err) { console.error(`Failed to put data. Code: ${err.code},message:${err.message}`); } try { preferences.flush((err) => { if (err) { console.error(`Failed to flush. Code:${err.code}, message:${err.message}`); return; } console.info('Succeeded in flushing.'); }) } catch (err) { console.error(`Failed to flush. Code:${err.code}, message:${err.message}`); }
import router from '@ohos.router'; PersistentStorage.persistProp("time", "Hello, OpenHarmony") @Entry @Component struct ComponentTest { // 初始化time1,如果AppStorage @StorageLink('time') time1: string = "1648643734154"; @StorageLink('time') time2: string = "OpenHarmony"; build() { Column({ space: 10 }) { Text(`父组件【${this.time1}】`) .fontSize(20) .backgroundColor(Color.Pink) Button('更新时间') .onClick(() => { this.time2 = new Date().getTime().toString(); }) Button('跨页面数据绑定') .onClick(() => { router.pushUrl({ url: "pages/test/setting" }) }) } .width('100%') .height('100%') .padding(10) } }