{"id":15512,"date":"2024-01-11T18:31:32","date_gmt":"2024-01-11T10:31:32","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=15512"},"modified":"2024-01-11T18:31:32","modified_gmt":"2024-01-11T10:31:32","slug":"%e7%94%9f%e6%88%90%e7%bd%91%e9%a1%b5%e7%89%88app%e4%bd%9c%e7%94%a8%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/15512\/","title":{"rendered":"\u751f\u6210\u7f51\u9875\u7248app\u4f5c\u7528\u4ecb\u7ecd"},"content":{"rendered":"

\u7f51\u9875\u7248App\uff08\u4e5f\u79f0\u4f5cWeb App\u3001PWA-Progressive Web App\uff09\u662f\u4e00\u79cd\u4ecb\u4e8e\u666e\u901a\u7f51\u7ad9\uff08\u57fa\u4e8eHTML\uff0cCSS\u548cJavaScript\u6784\u5efa\uff09\u548c\u539f\u751f\u5e94\u7528\uff08\u57fa\u4e8eAndroid\uff0ciOS\u7b49\u7279\u5b9a\u5e73\u53f0\u6784\u5efa\uff09\u4e4b\u95f4\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u80fd\u591f\u4ee5\u539f\u751f\u5e94\u7528\u7684\u5f62\u5f0f\u5728\u8bbe\u5907\u4e0a\u8fd0\u884c\uff0c\u540c\u65f6\u5411\u7528\u6237\u63d0\u4f9b\u5feb\u901f\u7684\u5b89\u88c5\u548c\u66f4\u65b0\u4f53\u9a8c\uff0c\u540c\u65f6\u964d\u4f4e\u4e86\u5f00\u53d1\u548c\u7ef4\u62a4\u6210\u672c\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u7f51\u9875\u7248App\u7684\u57fa\u672c\u539f\u7406\u4ee5\u53ca\u5982\u4f55\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\u5c06\u73b0\u6709\u7684\u7f51\u7ad9\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7f51\u9875\u7248App\u3002<\/p>\n

\u4e00\u3001\u7f51\u9875\u7248App\u7684\u57fa\u672c\u539f\u7406<\/p>\n

1. \u6e10\u8fdb\u5f0f\uff1a\u7f51\u9875\u7248App\u662f\u6784\u5efa\u5728\u73b0\u6709\u7f51\u7ad9\u6280\u672f\u4e4b\u4e0a\u7684\uff0c\u53ef\u4ee5\u5728\u4e0d\u635f\u5bb3\u666e\u901a\u7f51\u7ad9\u529f\u80fd\u548c\u4f53\u9a8c\u7684\u57fa\u7840\u4e0a\uff0c\u9010\u6b65\u5411\u7528\u6237\u63d0\u4f9b\u539f\u751f\u5e94\u7528\u7279\u6027\u3002<\/p>\n

2. \u54cd\u5e94\u5f0f\u8bbe\u8ba1\uff1a\u4e3a\u4e86\u8ba9Web App\u5728\u684c\u9762\u3001\u5e73\u677f\u548c\u624b\u673a\u7b49\u591a\u79cd\u8bbe\u5907\u4e0a\u90fd\u80fd\u63d0\u4f9b\u826f\u597d\u7684\u4f53\u9a8c\uff0c\u91c7\u7528\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u81f3\u5173\u91cd\u8981\u3002<\/p>\n

3. \u79bb\u7ebf\u8bbf\u95ee\uff1a\u501f\u52a9Service Worker\u6280\u672f\uff0c\u7f51\u9875\u7248App\u53ef\u4ee5\u5728\u79bb\u7ebf\u72b6\u6001\u4e0b\u8bbf\u95ee\uff0c\u7c7b\u4f3c\u4e8e\u539f\u751f\u5e94\u7528\u3002<\/p>\n

4. \u5e94\u7528\u6e05\u5355\uff1a\u4e00\u4e2a\u5728JSON\u683c\u5f0f\u7684\u6587\u4ef6\uff08manifest.json\uff09\u4e2d\u5b9a\u4e49\u4e86\u5e94\u7528\u540d\u79f0\u3001\u56fe\u6807\u3001\u5165\u53e3\u9875\u9762\u7b49\u4fe1\u606f\uff0c\u4f7f\u5f97\u7f51\u9875\u7248App\u53ef\u4ee5\u88ab\u6dfb\u52a0\u5230\u684c\u9762\uff0c\u5e76\u50cf\u539f\u751f\u5e94\u7528\u4e00\u6837\u542f\u52a8\u3002<\/p>\n

\u4e8c\u3001\u5c06\u73b0\u6709\u7f51\u7ad9\u8f6c\u6362\u4e3a\u7f51\u9875\u7248App<\/p>\n

1. \u521b\u5efa\u5e94\u7528\u6e05\u5355\uff08manifest.json\uff09\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`json<\/p>\n

{<\/p>\n

“name”: “My Web App”,<\/p>\n

“short_name”: “WebApp”,<\/p>\n

“description”: “A sample web app.”,<\/p>\n

“start_url”: “\/index.html”,<\/p>\n

“display”: “standalone”,<\/p>\n

“background_color”: “#ffffff”,<\/p>\n

“theme_color”: “#4CAF50”,<\/p>\n

“icons”: [<\/p>\n

{<\/p>\n

“src”: “icons\/icon-72×72.png”,<\/p>\n

“sizes”: “72×72”,<\/p>\n

“type”: “image\/png”<\/p>\n

}<\/p>\n

]<\/p>\n

}<\/p>\n

“`<\/p>\n

2. \u5728HTML\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5bf9manifest.json\u7684\u5f15\u7528\uff1a<\/p>\n

“`html<\/p>\n<\/p>\n

\n

\n

\n

…<\/p>\n<\/p>\n

“`<\/p>\n

3. \u5728\u7f51\u7ad9\u4e2d\u6ce8\u518cService Worker\uff1a<\/p>\n

\u521b\u5efa\u4e00\u4e2aJavaScript\u6587\u4ef6\uff0c\u4f8b\u5982`service-worker.js`\uff0c\u5b9a\u4e49\u7f13\u5b58\u7b56\u7565\u4ee5\u53ca\u79bb\u7ebf\u8bbf\u95ee\u7684\u884c\u4e3a\u3002<\/p>\n

“`javascript<\/p>\n

self.addEventListener(“install”, (event) => {<\/p>\n

event.waitUntil(<\/p>\n

caches.open(“my-cache”).then((c\u4e00\u952e\u751f\u6210app\u56fe\u6807\u7f51\u7ad9<\/a>ache) => {<\/p>\n

return cache.addAll([<\/p>\n

“\/”,<\/p>\n

“\/index.html”,<\/p>\n

“\/styles.css”,<\/p>\n

“\/app.js”,<\/p>\n

“\/icons\/icon-72×72.png”,<\/p>\n

]);<\/p>\n

})<\/p>\n

);<\/p>\n

});<\/p>\n

self.addEventListener(“fetch”, (event) => {<\/p>\n

event.respondWith(<\/p>\n

caches.match(event.request).then((response) => {<\/p>\n

return response || fetch(event.request);<\/p>\n

})<\/p>\n

);<\/p>\n

});<\/p>\n

“`<\/p>\n

\u5728\u4e3b\u9875\u9762\uff08\u4f8b\u5982`app.js`\uff09\u4e2d\u6ce8\u518cS<\/p>\n

<\/figure>\n<\/p>\n

ervice Worker\u3002<\/p>\n

“`javascript<\/p>\n

if (“serviceWorker” in navigator) {<\/p>\n

navigator.serviceWorker<\/p>\n

.register(“\/service-worker.js”)<\/p>\n

.then((registration) => {<\/p>\n

console.log(“Service Worker registered with scope:”, registration.scope);<\/p>\n

})<\/p>\n

.catch((error) => {<\/p>\n

console.error(“Service Worker registration faapp\u5728\u7ebf\u751f\u6210\u7cfb\u7edf<\/a>iled:”, error);<\/p>\n

});<\/p>\n

}<\/p>\n

“`<\/p>\n

4. \u4f18\u5316\u54cd\u5e94\u5f0f\u8bbe\u8ba1\uff1a<\/p>\n

\u9488\u5bf9\u4e0d\u540c\u8bbe\u5907\u548c\u5c4f\u5e55\u5c3a\u5bf8\uff0c\u4f7f\u7528CSS\u5a92\u4f53\u67e5\u8be2\u8fdb\u884c\u6837\u5f0f\u8c03\u6574\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u8bbe\u5907\u7279\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n

“`css<\/p>\n

\/* \u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u4f18\u5316\u5e03\u5c40 *\/<\/p>\n

@media screen and (max-width: 600px) {<\/p>\n

body {<\/p>\n

font-size: 14px;<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u7ecf\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u539f\u672c\u7684\u7f51\u7ad9\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u7f51\u9875\u7248App\u8fd0\u884c\uff0c\u5177\u5907\u6dfb\u52a0\u5230\u684c\u9762\u3001\u79bb\u7ebf\u8bbf\u95ee\u548c\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u7b49\u7279\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7f51\u9875\u7248App\uff08\u4e5f\u79f0\u4f5cWeb App\u3001PWA-Progressive Web App\uff09\u662f\u4e00\u79cd\u4ecb\u4e8e\u666e\u901a\u7f51\u7ad9\uff08\u57fa\u4e8eHTML\uff0cCSS\u548cJavaScript\u6784\u5efa\uff09\u548c\u539f\u751f\u5e94\u7528\uff08\u57fa\u4e8eAndroid\uff0ciOS\u7b49\u7279\u5b9a\u5e73<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[12480,19352,259,247,149],"topic":[],"class_list":{"0":"post-15512","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-appkf","7":"tag-app","9":"tag-259","10":"tag-247","11":"tag-149"},"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/15512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=15512"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/15512\/revisions"}],"predecessor-version":[{"id":15589,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/15512\/revisions\/15589"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=15512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=15512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=15512"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=15512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}