中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
ValueType怎么转成下面的数据来使用number | string | boolean | Array<number> | Array<string> | Array<boolean> | Uint8Array | object | bigint。
微信扫码分享
import data_preferences from '@ohos.data.preferences' const PREFERENCES_NAME = "my_pre" export default class PreUtil { private static instance: PreUtil; private preferences; private constructor(context) { this.getPre(context) } // 获取单例对象 public static getInstance(context) { if (this.instance == null) { this.instance = new PreUtil(context) } return this.instance } // 获取preferences对象 getPre(context) { let promise = data_preferences.getPreferences(context, PREFERENCES_NAME) promise.then((preferences) => { console.info("PreUtil Get preferences successfully.") this.preferences = preferences }).catch((err) => { console.info("PreUtil Get preferences failed. err:" + err) }) } // 保存数据 putVal(key: string, val) { let promise = this.preferences.put(key, val) promise.then(() => { console.info("PreUtil Put value of " + key + " successfully.") let promise = this.preferences.flush() promise.then(() => { console.info("PreUtil Flushed to file successfully.") }).catch((err) => { console.info("PreUtil Flush to file failed, err: " + err) }) }).catch((err) => { console.info("PreUtil Put value of startup failed, err: " + err) }) } // 获取数据 getVal(key: string, defaultVal) { let promise = this.preferences.get(key, defaultVal) promise.then((value) => { console.info("PreUtil Get value of " + key + " is " + value) }).catch((err) => { console.info("PreUtil Get value of " + key + " failed, err: " + err) }) } // 删除数据 deleteVal(key: string) { let promiseDel = this.preferences.delete(key) promiseDel.then(() => { console.info("PreUtil Deleted key " + key + " successfully.") }).catch((err) => { console.info("PreUtil Deleted key " + key + " failed with err:" + err) }) } protected getBoolean(key: string, defValue: boolean): boolean { return this.preferences.getSync(key, defValue) } protected getString(key: string, defValue: string): string { return this.preferences.getSync(key, defValue).toString(); } protected getBoolean1(key: string, defValue: boolean): boolean { return this.preferences.getSync(key, defValue) } } 请参考如下代码: import dataPreferences from '@ohos.data.preferences'; import { Context } from '@kit.AbilityKit'; export class PreferencesBase { private TAG_U = 'PreferencesBase'; private pref: dataPreferences.Preferences; constructor(p_name: string,context: Context) { let options: dataPreferences.Options = { name: p_name} this.pref = dataPreferences.getPreferencesSync(context, options); } protected putString(key: string, value: string) { try { this.pref.putSync(key, value); } catch (err) { // JLog.error(this.TAG_U, 'put the putString failed, err: key=' + key, err); } try { this.pref.flush(); } catch (err) { // JLog.error(this.TAG_U, 'put the flush putString failed, err: key=' + key, err); } } protected getString(key: string, defValue: string): string { return this.pref.getSync(key, defValue).toString(); } protected putNumber(key: string, value: number) { try { this.pref.putSync(key, value); } catch (err) { // JLog.error(this.TAG_U, 'put the putNumber failed, err: key=' + key, err); } try { this.pref.flush(); } catch (err) { // JLog.error(this.TAG_U, 'put the flush putNumber failed, err: key=' + key, err); } } protected getNumber(key: string, defValue: number):number { return this.pref.getSync(key, defValue) as number } protected putBoolean(key: string, value: boolean) { try { this.pref.putSync(key, value); } catch (err) { // JLog.error(this.TAG_U, 'put the putBoolean failed, err: key=' + key, err); } try { this.pref.flush(); } catch (err) { // JLog.error(this.TAG_U, 'put the flush putBoolean failed, err: key=' + key, err); } } protected getBoolean(key: string, defValue: boolean):boolean { return this.pref.getSync(key, defValue) as boolean } }