{"id":1000,"date":"2024-06-11T19:39:07","date_gmt":"2024-06-11T11:39:07","guid":{"rendered":"https:\/\/gjweb.top\/?p=1000"},"modified":"2024-06-11T19:39:10","modified_gmt":"2024-06-11T11:39:10","slug":"harmonyos-4-0-store%e5%b0%81%e8%a3%85%e6%9c%ac%e5%9c%b0%e5%ad%98%e5%82%a8","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=1000","title":{"rendered":"HarmonyOS 4.0 store\u5c01\u88c5\u672c\u5730\u5b58\u50a8"},"content":{"rendered":"\n<p>\u5229\u7528 entryAbility \u5e94\u7528\u8bc1\u660e\u5468\u671f\u7684\u7279\u70b9\uff0c\u521d\u59cb\u5316 store \u5b9e\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import UIAbility from '@ohos.app.ability.UIAbility';<br>import hilog from '@ohos.hilog';<br>import window from '@ohos.window';<br>import dataPreferences from '@ohos.data.preferences';<br>export default class EntryAbility extends UIAbility {<br> &nbsp; &nbsp;onCreate(want, launchParam) {<br> &nbsp; &nbsp; &nbsp; &nbsp;try{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataPreferences.getPreferences(this.context,\"myStore\",(err,preferences)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(err){console.error(\"\u62a5\u9519\u4e86\");return}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/*\u4f7f\u7528 globalThis \u65b9\u5f0f\u53ef\u4ee5\u8ba9\u5168\u5c40\u5e94\u7528\u516c\u7528\u8be5\u5c5e\u6027*\/<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;globalThis.entryAbilityPreferences = preferences;<br> &nbsp; &nbsp; &nbsp; &nbsp;  })<br> &nbsp; &nbsp; &nbsp;  }catch (error){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.error(\"\u62a5\u9519\u4e86\")<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<p>\u5c01\u88c5 store \u7c7b\uff0c<code>src\\main\\ets\\utils\\storage.ets<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default class Storage{<br> &nbsp;entryAbilityPreferences;<br> &nbsp;constructor() {<br> &nbsp; &nbsp;this.entryAbilityPreferences = globalThis.entryAbilityPreferences;<br>  }<br> &nbsp;\/*\u8bbe\u7f6e*\/<br> &nbsp;setItem(key:string,value:string|number|boolean){<br> &nbsp; &nbsp;this.entryAbilityPreferences.has(key,(err, val)=&gt;{<br> &nbsp; &nbsp; &nbsp;if(err){console.error(\"set\u8c03\u7528\u5931\u8d25\");return}<br> &nbsp; &nbsp; &nbsp;if(val){console.error(\"key\u540d\u5df2\u5b58\u5728\")}else{<br> &nbsp; &nbsp; &nbsp; &nbsp;try{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.entryAbilityPreferences.put(key,value)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.entryAbilityPreferences.flush()<br> &nbsp; &nbsp; &nbsp;  }catch (err){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.error(\"put\u65b9\u6cd5\u62a5\u9519\u4e86\")<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp;  }<br> &nbsp;  })<br>  }<br> &nbsp;\/*\u67e5\u627e*\/<br> &nbsp;getItem(key:string,defaultVal:string|number|boolean):Promise&lt;string|number|boolean&gt;{<br> &nbsp; &nbsp;return new Promise((resolve,reject)=&gt;{<br> &nbsp; &nbsp; &nbsp;this.entryAbilityPreferences.get(key,defaultVal,(err,val)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;if(err){reject(\"\u62a5\u9519\u4e86\")}<br> &nbsp; &nbsp; &nbsp; &nbsp;resolve(val as string|number|boolean);<br> &nbsp; &nbsp;  })<br> &nbsp;  })<br>  }<br> &nbsp;\/*\u5220\u9664*\/<br> &nbsp;delItem(key:string){<br> &nbsp; &nbsp;this.entryAbilityPreferences.delete(key)<br>  }<br>}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import router from '@ohos.router';<br>import promptAction from '@ohos.promptAction'<br>import Storage from \"..\/utils\/storage\"<br>\u200b<br>@Entry<br>@Component<br>struct Page {<br> &nbsp; &nbsp;storage = new Storage();<br> &nbsp; &nbsp;@Builder Header(){<br> &nbsp; &nbsp; &nbsp; &nbsp;Row(){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button(\"\u5b58\u50a8\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  .onclick(async ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.storage.setItem(\"userName\",\"\u9f9a\u7bad\") \/*\u5199\u5165*\/<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let result = await this.storage.getItem(\"userName\",\"\u767b\u5f55\") \/*\u8bfb\u53d6*\/<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  })<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528 entryAbility \u5e94\u7528\u8bc1\u660e\u5468\u671f\u7684\u7279\u70b9\uff0c\u521d\u59cb\u5316 store \u5b9e\u4f8b \u5c01\u88c5 store \u7c7b\uff0csrc\\m [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-1000","post","type-post","status-publish","format-standard","hentry","category-harmonyos4-0"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/1000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1000"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/1000\/revisions"}],"predecessor-version":[{"id":1001,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/1000\/revisions\/1001"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}