HarmonyOS PersistentStorage持久化不生效?

如题:HarmonyOS PersistentStorage持久化不生效?

阅读 530
1 个回答

参考示例修改:

import { Constants as Const } from '../../constants/Constants'
import { router } from '@kit.ArkUI';
import { LastTimeUpdateComp } from '../../view/LastTimeUpdateComp';
import { SettingTopBar } from '../../view/SettingTopBar';
import { SettingCommonItem } from '../../view/SettingCommonItem';
import { preferences } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
import { NumFormatUtil } from '../../util/NumFormatUtil'

let dataPreferences: preferences.Preferences | null = null;
PersistentStorage.persistProp('symbolshow', '')

@Entry
@Component
struct CalcSettingPage {
  localTips: boolean = true
  context = getContext(this) as common.UIAbilityContext
  @State rateBase: number = 100
  @State showCurrencySymbol: boolean = true
  @StorageLink('symbolshow') symbolshow: string = ''

  aboutToAppear(): void {
    let options: preferences.Options = { name: Const.PREFERENCES_DEFAULT_USER_KEY };
    dataPreferences = preferences.getPreferencesSync(this.context, options)
  }

  onPageShow(): void {
    this.rateBase = dataPreferences?.getSync(Const.PREFERENCES_RATE_BASE, 100) as number
    console.info('symbolshow==' + this.symbolshow)
    if (this.symbolshow === 'false') {
      this.showCurrencySymbol = false
    } else {
      this.showCurrencySymbol = true
    }
  }

  build() {
    Column({ space: 20 }) {
      SettingTopBar({ title: '换算设置' })
        .margin({ left: 20 })

      Scroll() {
        Column() {
          // 上次更新
          LastTimeUpdateComp()
          Divider()
          //货币切换
          Column() {
            Row() {
              Text('当地货币')
              Image($r('app.media.ic_setting_question'))
                .width(20)
                .onClick(() => {
                  // this.localTips = !this.localTips
                })
              Blank()
              Toggle({ type: ToggleType.Switch, isOn: true })
                .selectedColor(Color.Black)
            }.width(Const.FULL_WIDTH)

            Row() {
              Text('货币符号')
              Blank()
              Toggle({ type: ToggleType.Switch, isOn: this.showCurrencySymbol })
                .selectedColor(Color.Black)
                .onChange((isOn: boolean) => {
                  if (isOn == true) {
                    AppStorage.setOrCreate('symbolshow', 'true')
                  } else {
                    AppStorage.setOrCreate('symbolshow', 'false')
                  }
                  // AppStorage.setOrCreate(Const.APPSTORAGE_CURRENCY_SYMBOL, isOn)
                  this.showCurrencySymbol = isOn
                })
            }.width(Const.FULL_WIDTH)
          }
          // .backgroundColor(Color.Pink)
          .height(2 * Const.CALC_SETTING_PAGE_ITEM_HEIGHT)
          .justifyContent(FlexAlign.SpaceEvenly)

          Divider()

          // 货币默认值
          Column() {
            Row() {
              Text('货币默认值')
              Blank()
              Text(NumFormatUtil.thousandsFormat(this.rateBase + ''))
                .fontColor(Color.Gray)
              Image($r('app.media.ic_setting_right_arrow'))
                .width(10)
                .margin({ left: 10, top: 10, bottom: 10 })
            }
            .width(Const.FULL_WIDTH)
            .height(Const.CALC_SETTING_PAGE_ITEM_HEIGHT)
            .onClick(() => {
              router.pushUrl({
                url: 'pages/calc/CalcSettingCurrencyDefaultPage'
              })
            })

            SettingCommonItem({ text: '小数点位数' })
              .onClick(() => {
                router.pushUrl({
                  url: 'pages/calc/CalcSettingDecimalPage'
                })
              })
            Divider()
          }

          // 主题皮肤
          Column() {
            SettingCommonItem({ text: '主题皮肤' })
            Divider()
          }

          // 更多设置
          Column() {
            SettingCommonItem({ text: '更多设置' })
            SettingCommonItem({ text: '换算小助手' })
            Divider()
          }

          Row() {
            Text('恢复至默认币种列表')
          }
          .height(Const.CALC_SETTING_PAGE_ITEM_HEIGHT)
        }
      }
      .width(Const.THOUSANDTH_900)
    }
    .height('100%')
    .width('100%')
    .backgroundColor(Color.White)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进