{"id":5799,"date":"2018-07-15T00:21:38","date_gmt":"2018-07-14T15:21:38","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=5799"},"modified":"2023-05-27T21:39:22","modified_gmt":"2023-05-27T12:39:22","slug":"%e3%80%90nuxt-js%e3%80%91%e3%80%8c%e3%83%9a%e3%83%bc%e3%82%b8%e3%82%b3%e3%83%b3%e3%83%9d%e3%83%bc%e3%83%8d%e3%83%b3%e3%83%88%e3%80%8d%e3%81%ae%e5%b1%9e%e6%80%a7%e3%80%8casyncdata%e3%80%8d%e3%81%ab","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=5799","title":{"rendered":"\u3010Nuxt.js\u3011\u300c\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u300d\u306e\u5c5e\u6027\u300casyncData\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>asyncData\u5c5e\u6027\u3068\u306f\uff1f<\/h2>\n<p>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30ed\u30fc\u30c9\u6bce\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>Vuex\u30b9\u30c8\u30a2\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u308f\u305a\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3001\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306e\u524d\u306b\u3001\u975e\u540c\u671f\u51e6\u7406<\/strong><\/span>\u3092\u884c\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>\u6a19\u6e96\u306eVue.js\u3067\u306f\u975e\u540c\u671f\u51e6\u7406\u7528\u306e\u69cb\u6587\u306f\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u306e\u3067\u3001Nuxt.js\u3067\u306f\u975e\u540c\u671f\u51e6\u7406\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002<\/p>\n<h2>asyncData\u306e\u7279\u5fb4<\/h2>\n<p>asyncData\u5185\u3067\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306f\u3001\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306edata\u3068\u30de\u30fc\u30b8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>asyncData\u306e\u5b9f\u88c5\u65b9\u6cd5<\/h2>\n<h3>asyncData\u306e\u7b2c\u4e00\u5f15\u6570<\/h3>\n<p>\u7b2c\u4e00\u5f15\u6570\u306b\u306f\u3001App\u3084\u3001env\u7b49\u306e<span style=\"color: #ff0000;\"><strong>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/strong><\/span>\u3092\u3068\u308a\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u3064\u3044\u3066\u306f\u3001\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"M2zmLb3gPl\"><p><a href=\"http:\/\/www.code-magagine.com\/?p=5779\">\u3010Nuxt.js\u3011\u300c\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u300d\uff08asyncData\u3001fetch\u3001\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3001layout\uff09<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/www.code-magagine.com\/?p=5779&#038;embed=true#?secret=M2zmLb3gPl\" data-secret=\"M2zmLb3gPl\" width=\"500\" height=\"282\" title=\"&#8220;\u3010Nuxt.js\u3011\u300c\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u300d\uff08asyncData\u3001fetch\u3001\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3001layout\uff09&#8221; &#8212; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30de\u30ac\u30b8\u30f3\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>asyncData\u306e\u5b9f\u88c5\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001\u4e0b\u8a18\u306e3\u901a\u308a\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>1.Promise\u3092\u8fd4\u3059\u3002<\/h3>\n<pre class=\"lang:default decode:true\">&lt;template&gt;\r\n&lt;section&gt;\r\n&lt;h1&gt;sample&lt;\/h1&gt;\r\n{{msg}}\r\n&lt;\/section&gt;\r\n&lt;\/template&gt;\r\n\r\n&lt;script&gt;\r\nexport default{\r\nasyncData(params){\r\n\treturn new Promise((resolve, reject) =&gt; {\r\n\t\t\/\/\u975e\u540c\u671f\u51e6\u7406\r\n\t\tresolve();\r\n\t}).then(() =&gt; {\r\n\t\treturn {msg:\"test\"};\r\n\t});\r\n}};\r\n&lt;\/script&gt;<\/pre>\n<p>return\u3067\u30de\u30fc\u30b8\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>2.async\/await\u3092\u5229\u7528\u3059\u308b\u3002<\/h3>\n<pre class=\"lang:default decode:true\">&lt;template&gt;\r\n&lt;section&gt;\r\n&lt;h1&gt;sample&lt;\/h1&gt;\r\n{{msg}}\r\n&lt;\/section&gt;\r\n&lt;\/template&gt;\r\n\r\n&lt;script&gt;\r\nasync function fn(){\r\n\treturn {msg:\"test\"};\r\n}\r\n\r\nexport default{\r\n\tasync asyncData(params){\r\n\t\tconst result = await fn();\r\n\t\treturn result;\r\n\t}\r\n};\r\n&lt;\/ script &gt;<\/pre>\n<h3>3.\u7b2c\u4e8c\u5f15\u6570\u3068\u3057\u3066\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u5229\u7528\u3059\u308b\u3002<\/h3>\n<pre class=\"lang:default decode:true\">&lt;template&gt;\r\n&lt;section&gt;\r\n&lt;h1&gt;sample&lt;\/h1&gt;\r\n{{msg}}\r\n&lt;\/section&gt;\r\n&lt;\/template&gt;\r\n\r\n&lt;script&gt;\r\nasync function fn(){\r\n\treturn {msg:\"test\"};\r\n}\r\n\r\nexport default{\r\nasyncData({params}, callback){\r\n\tfn().then(\r\n\t\tres =&gt; {callback(null, {msg: res.msg});\r\n\t});\r\n}};\r\n&lt;\/script&gt;<\/pre>\n<h3>\u5b9f\u884c\u7d50\u679c<\/h3>\n<p>\u3044\u305a\u308c\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3082\u3001\u4e0b\u8a18\u306e\u5b9f\u884c\u7d50\u679c\u306b\u306a\u308b\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-5870\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/07\/asyncData.png\" alt=\"\" width=\"277\" height=\"133\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"asyncData\u5c5e\u6027\u3068\u306f\uff1f \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30ed\u30fc\u30c9\u6bce\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002Vuex\u30b9\u30c8\u30a2\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u308f\u305a\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3001\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306e\u524d\u306b\u3001\u975e\u540c\u671f\u51e6\u7406\u3092\u884c\u3063\u3066\u304f\u308c\u307e\u3059\u3002 \u6a19\u6e96\u306eVue.js\u3067\u306f\u975e\u540c\u671f\u51e6\u7406\u7528\u306e\u69cb\u6587\u306f\u3042\u308a\u307e\u305b\u3093 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/5799"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5799"}],"version-history":[{"count":16,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/5799\/revisions"}],"predecessor-version":[{"id":20380,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/5799\/revisions\/20380"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5799"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}