|
typescript import { preferences } from "@kit.ArkData"
@Entry @Component struct PreferencesPage {
defaultStore: string = "DefaultStore" oneStore: string = "oneStore" tKey: string = "tKey" @State showTxt: string = "" setToken(token: string, storeName: string = this.defaultStore) { const store = preferences.getPreferencesSync(getContext(), { name: storeName }) store.putSync(this.tKey, token) store.flushSync() }
aboutToAppear(): void { this.setToken("你好") this.showTxt = preferences.getPreferencesSync(getContext(), { name: this.defaultStore }).getSync(this.tKey, "") as string }
build() { Column() { Text(this.showTxt) .fontSize(24) } } }
class PreferencesClass { defaultStore: string = 'xxxx' firstStore: string = 'xxx' // 字段名称,一个字段配2个方法,读取和写入 tokenKey:string = 'TOKEN_KEY' // 向仓库中存储内容 setToken(content: Context, token: string, storeName: string = this.defaultStore){ const store = preferences.getPreferencesSync(content, {name: storeName}) store.putSync(this.defaultStore,token) store.flush() //存储 } // 读取仓库中的内容 getToken(content: Context,storeName: string = this.defaultStore){ const store = preferences.getPreferencesSync(content, {name: storeName}) return store.getSync(this.tokenKey, '') } }
|