{"id":32536,"date":"2024-11-25T10:06:19","date_gmt":"2024-11-25T02:06:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32536\/"},"modified":"2024-11-25T10:06:19","modified_gmt":"2024-11-25T02:06:19","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b8%ad%e4%bd%bf%e7%94%a8async-await%e8%af%ad%e6%b3%95%e7%9a%84%e6%96%b9%e6%b3%95%ef%bc%88%e4%bb%a3%e7%a0%81%e7%a4%ba%e4%be%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32536\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528async\/await\u8bed\u6cd5\u7684\u65b9\u6cd5\uff08\u4ee3\u7801\u793a\u4f8b\uff09"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528async\/await\u8bed\u6cd5\u7684\u65b9\u6cd5\uff08\u4ee3\u7801\u793a\u4f8b\uff09\uff0c\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>1\u3001\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u9879\u76ee\u6dfb\u52a0package.json\u6587\u4ef6\u6216\u8005\u76f4\u63a5npm init.<\/p>\n<p>2.\u5728package.json\u4e2d\u6dfb\u52a0regenerator\u5305\u548c\u7248\u672c<\/p>\n<pre>`\"devDependencies\": {\n\"regenerator\":\"0.13.3\"}`<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>3.\u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177-\u300b\u5de5\u5177-\u300bnpm\u6784\u5efa<\/p>\n<p>4.\u5728\u9700\u8981\u4f7f\u7528async\/await\u8bed\u6cd5\u7684\u6587\u4ef6\u5f15\u5165regeneratorRuntime<\/p>\n<pre>const regeneratorRuntime = require('regenerator-runtime')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>5.\u4f7f\u7528async\/await\u8bed\u6cd5<\/p>\n<pre>lifetimes: {\n    attached:async function(){\n      \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u8fdb\u5165\u9875\u9762\u8282\u70b9\u6811\u65f6\u6267\u884c\n     let data= await req(this.properties.apiType);\n     console.log(data)\n    },\n    detached() {\n      \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u88ab\u4ece\u9875\u9762\u8282\u70b9\u6811\u79fb\u9664\u65f6\u6267\u884c\n    },\n  },<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u9700\u8981\u6ce8\u610fthis\u7684\u6307\u5411\uff0c\u6bd4\u5982\u751f\u547d\u5468\u671f\u5982\u679c\u7528\u7bad\u5934\u51fd\u6570\u4f1a\u4e22\u5931this<br \/>\u6216\u8005\u76f4\u63a5\u8fd9\u6837<\/p>\n<pre> lifetimes: {\n    async attached(){\n      \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u8fdb\u5165\u9875\u9762\u8282\u70b9\u6811\u65f6\u6267\u884c\n     let data= await req(this.properties.apiType);\n     console.log(data)\n    },\n    async detached() {\n      \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u88ab\u4ece\u9875\u9762\u8282\u70b9\u6811\u79fb\u9664\u65f6\u6267\u884c\n     \n    },\n  },<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u5b9e\u5728app.js require\u4e00\u6b21\u5305\u5168\u5c40\u90fd\u80fd\u7528\u4e86<\/p>\n<p>\u672c\u6587\u53c2\u8003\uff1a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u57fa\u7840\u6559\u7a0b&nbsp;https:\/\/www.html.cn\/study\/20.html<\/p>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528async\/await\u8bed\u6cd5\u7684\u65b9\u6cd5\uff08\u4ee3\u7801\u793a\u4f8b\uff09\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528async\/await\u8bed\u6cd5\u7684\u65b9\u6cd5\uff08\u4ee3\u7801\u793a\u4f8b\uff09\uff0c\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002 1\u3001\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u9879\u76ee\u6dfb\u52a0package.json\u6587\u4ef6\u6216\u8005\u76f4\u63a5npm init. 2.\u5728package.json\u4e2d\u6dfb\u52a0regenerator\u5305\u548c\u7248\u672c `&#8221;devDependencies&#8221;: { &#8220;regenerator&#8221;:&#8221;0.13.3&#8243;}` \u767b\u5f55\u540e\u590d\u5236 3.\u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177-\u300b\u5de5\u5177-\u300bnpm\u6784\u5efa 4.\u5728\u9700\u8981\u4f7f\u7528async\/await\u8bed\u6cd5\u7684\u6587\u4ef6\u5f15\u5165regeneratorRuntime const regeneratorRuntime = require(&#8216;regenerator-runtime&#8217;) \u767b\u5f55\u540e\u590d\u5236 5.\u4f7f\u7528async\/await\u8bed\u6cd5 lifetimes: { attached:async function(){ \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u8fdb\u5165\u9875\u9762\u8282\u70b9\u6811\u65f6\u6267\u884c let data= await req(this.properties.apiType); console.log(data) }, detached() { \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u88ab\u4ece\u9875\u9762\u8282\u70b9\u6811\u79fb\u9664\u65f6\u6267\u884c }, }, \u767b\u5f55\u540e\u590d\u5236 \u9700\u8981\u6ce8\u610fthis\u7684\u6307\u5411\uff0c\u6bd4\u5982\u751f\u547d\u5468\u671f\u5982\u679c\u7528\u7bad\u5934\u51fd\u6570\u4f1a\u4e22\u5931this\u6216\u8005\u76f4\u63a5\u8fd9\u6837 lifetimes: { async attached(){ \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u8fdb\u5165\u9875\u9762\u8282\u70b9\u6811\u65f6\u6267\u884c let data= await req(this.properties.apiType); console.log(data) }, async detached() { \/\/ \u5728\u7ec4\u4ef6\u5b9e\u4f8b\u88ab\u4ece\u9875\u9762\u8282\u70b9\u6811\u79fb\u9664\u65f6\u6267\u884c }, }, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-32536","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32536"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32536\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}