{"id":5779,"date":"2018-07-14T22:24:06","date_gmt":"2018-07-14T13:24:06","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=5779"},"modified":"2023-05-27T21:40:19","modified_gmt":"2023-05-27T12:40:19","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%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=5779","title":{"rendered":"\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"},"content":{"rendered":"<h2>page\u30c7\u30a3\u30ec\u30af\u30c8\u30ea<\/h2>\n<p>.vue\u30d5\u30a1\u30a4\u30eb\u306f\u3001\u4e00\u3064\u306e\u30da\u30fc\u30b8\u3092\u6307\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u3092<span style=\"color: #ff0000;\"><strong>\u300c\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u300d<\/strong><\/span>\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<h3>\u8ffd\u52a0\u3059\u308b\u5c5e\u6027<\/h3>\n<p>Nuxt.js\u3067\u306f\u3001\u30e6\u30cb\u30d0\u30fc\u30b5\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306b.vue\u30d5\u30a1\u30a4\u30eb\u306b\u72ec\u81ea\u306e\u5c5e\u6027\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u5c5e\u6027<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>asyncData<\/td>\n<td>\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\u4e8b\u524d\u51e6\u7406<\/strong><\/span>\u3092\u884c\u3044\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>fetch<\/td>\n<td>\u30da\u30fc\u30b8\u304c\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u308b\u524d\u306b\u3001\u30c7\u30fc\u30bf\u3092\u30b9\u30c8\u30a2\u306b\u5165\u308c\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>head<\/td>\n<td>\u30da\u30fc\u30b8\u306b\u5bfe\u3057\u3066\u3001\u30e1\u30bf\u30bf\u30b0\u3092\u8a2d\u5b9a\uff08head\u30bf\u30b0\u5185\u306e\u8a2d\u5b9a\uff09\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>layout<\/td>\n<td>layout\u3092\u6307\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>transition<\/td>\n<td>\u30da\u30fc\u30b8\u7279\u5b9a\u306e\u30c8\u30e9\u30b8\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\uff08\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u9077\u79fb\u6642\u306e\u52d5\u304d\u306b\u3064\u3044\u3066\u5b9a\u7fa9\u3067\u304d\u308b\u3002\uff09<\/td>\n<\/tr>\n<tr>\n<td>scrollToTop<\/td>\n<td>\u30da\u30fc\u30b8\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u6642\u306b\u3001\u4e00\u756a\u4e0a\u307e\u3067\u30b9\u30af\u30ed\u30fc\u30eb\u3059\u308b\u304b\u5426\u304b\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>validate<\/td>\n<td>\u52d5\u7684\u306a\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u3059\u308b\u305f\u3081\u306e\u691c\u8a3c\u95a2\u6570\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>middleware<\/td>\n<td>\u30da\u30fc\u30b8\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u6642\u306b\u5b9f\u884c\u3059\u308b\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>asyncData\u306e\u4f7f\u3044\u65b9<\/h2>\n<p>asyncData\u306e\u5177\u4f53\u7684\u306a\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u306f\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"5uCkPZeyRu\"><p><a href=\"http:\/\/www.code-magagine.com\/?p=5799\">\u3010Nuxt.js\u3011\u300c\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u300d\u306e\u5c5e\u6027\u300casyncData\u300d\u306b\u3064\u3044\u3066<\/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=5799&#038;embed=true#?secret=5uCkPZeyRu\" data-secret=\"5uCkPZeyRu\" width=\"500\" height=\"282\" title=\"&#8220;\u3010Nuxt.js\u3011\u300c\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u300d\u306e\u5c5e\u6027\u300casyncData\u300d\u306b\u3064\u3044\u3066&#8221; &#8212; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30de\u30ac\u30b8\u30f3\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<h2>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h2>\n<p>Nuxt.js\u306e<span style=\"color: #ff0000;\"><strong>\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u4f7f\u7528\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u4e3b\u306b\u4f7f\u7528\u3055\u308c\u308b\u5834\u6240\u306f\u4e0b\u8a18\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>asyncData\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\u4e00\u5f15\u6570<\/li>\n<li>fetch\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\u4e00\u5f15\u6570<\/li>\n<li>layout\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\u4e00\u5f15\u6570<\/li>\n<li>\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u305f\u95a2\u6570\u306e\u7b2c\u4e00\u5f15\u6570<\/li>\n<\/ul>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306a\u5834\u6240\u3067\u3001<span style=\"color: #ff0000;\"><strong>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u901a\u3058\u3066\u30c7\u30fc\u30bf\u66f4\u65b0\u3092\u884c\u3046<\/strong><\/span>\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4e00\u89a7<\/h3>\n<table>\n<tbody>\n<tr>\n<th>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u540d<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>app<\/td>\n<td>Vue\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30eb\u30fc\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001\u5168\u3066\u306e\u30d7\u30e9\u30b0\u30a4\u30f3\u306b\u3053\u3053\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>isClient<\/td>\n<td>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3067\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u305f\u3089true\u306b\u306a\u308a\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>isServer<\/td>\n<td>\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u305f\u3089true\u306b\u306a\u308a\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>isStatic<\/td>\n<td>\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u9759\u7684\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3067\u51fa\u529b\u3055\u308c\u305f\u3089true\u306b\u306a\u308a\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>isDev<\/td>\n<td>\u958b\u767a\u30e2\u30fc\u30c9\u3067\u5b9f\u884c\u3055\u308c\u305f\u3089true\u306b\u306a\u308a\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>isHMR<\/td>\n<td>Webpack\u306e\u30db\u30c3\u30c8\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u51fa\u529b\u3055\u308c\u305f\u3089true\u306b\u306a\u308a\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>route<\/td>\n<td>vue-router\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9<\/td>\n<\/tr>\n<tr>\n<td>store<\/td>\n<td>Vuex\u306e\u30b9\u30c8\u30a2\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/td>\n<\/tr>\n<tr>\n<td>env<\/td>\n<td>nuxt.config.js\u306b\u5b9a\u7fa9\u3057\u305fenv\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53c2\u7167\u3067\u304d\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>params<\/td>\n<td>route\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306eparams\u306e\u30a8\u30a4\u30ea\u30a2\u30b9<\/td>\n<\/tr>\n<tr>\n<td>query<\/td>\n<td>route\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306equery\u306e\u30a8\u30a4\u30ea\u30a2\u30b9<\/td>\n<\/tr>\n<tr>\n<td>req<\/td>\n<td>http.Request\u3001Node.js\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3001\u9759\u7684\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3067\u51fa\u529b\u3057\u305f\u5834\u5408\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002<\/td>\n<\/tr>\n<tr>\n<td>res<\/td>\n<td>http.Response\u3001Node.js\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3001\u9759\u7684\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3067\u51fa\u529b\u3057\u305f\u5834\u5408\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002<\/td>\n<\/tr>\n<tr>\n<td>redirect<\/td>\n<td>\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u306e\u305f\u3081\u306e\u95a2\u6570\u3002\u4ed6\u306e\u30eb\u30fc\u30c8\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u305b\u305f\u3044\u5834\u5408\u306f\u3001redirect(status,path,params)\u3067\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u305b\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>error<\/td>\n<td>\u30a8\u30e9\u30fc\u30da\u30fc\u30b8\u3092\u8868\u793a\u3055\u305b\u308b\u305f\u3081\u306e\u95a2\u6570\u3002error(message,statusCode)\u3067\u5b9f\u884c\u3055\u305b\u307e\u3059\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>fetch\u3068\u306f\uff1f<\/h2>\n<p>\u30da\u30fc\u30b8\u304c\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u308b\u524d\u306b\u3001\u30c7\u30fc\u30bf\u3092store\u306b\u5165\u308c\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>fetch\u95a2\u6570<\/h3>\n<p>\u7b2c\u4e00\u5f15\u6570\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u53d7\u3051\u53d6\u308a\u3001<span style=\"color: #ff0000;\"><strong>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u3066store\u306b\u5165\u308c\u307e\u3059\u3002<\/strong><\/span><\/p>\n<p>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306b\u3064\u3044\u3066\u306f\u3001\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>fetch\u306e\u5229\u7528\u4f8b<\/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{{$store.state.msg}}&lt;\/section&gt;\r\n&lt;\/template&gt;\r\n\r\n&lt;script&gt;\r\nexport default{\r\n\tfetch ({store, params}){\r\n\t\treturn axios.get('http:\/\/xxx').then(() =&gt; {\r\n\t\t\tstore.commit('setMsg', {msg: res.msg})\r\n\t\t});\r\n\t}\r\n}\r\n&lt;\/script&gt;<\/pre>\n<h2>layout<\/h2>\n<p>layout\u306b\u306f\u4e0b\u8a182\u901a\u308a\u306e\u4f7f\u3044\u65b9\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u6587\u5b57\u5217\u3067layout\u3092\u6307\u5b9a\u3059\u308b\u3002<\/li>\n<li>\u95a2\u6570\u3067\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d7\u3051\u53d6\u308a\u3001\u52d5\u7684\u306b\u5b9a\u7fa9\u3092\u3059\u308b\u3002<\/li>\n<\/ul>\n<h3>\u69cb\u6587<\/h3>\n<pre class=\"lang:default decode:true \">&lt;script&gt;\r\n\texport default{\r\n\t\t\/\/\u9759\u7684\u306a\u30ec\u30a4\u30a2\u30a6\u30c8\r\n\t\tlayout: 'test',\r\n\t\tlayout (context){\r\n\t\t\/\/\u52d5\u7684\u306b\u8fd4\u3059\u3002\r\n\t\treturn 'test'\r\n\t}}\r\n&lt;\/script&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"page\u30c7\u30a3\u30ec\u30af\u30c8\u30ea .vue\u30d5\u30a1\u30a4\u30eb\u306f\u3001\u4e00\u3064\u306e\u30da\u30fc\u30b8\u3092\u6307\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u3092\u300c\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u300d\u3068\u547c\u3073\u307e\u3059\u3002 \u8ffd\u52a0\u3059\u308b\u5c5e\u6027 Nuxt.js\u3067\u306f\u3001\u30e6\u30cb\u30d0\u30fc\u30b5\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306b.vue\u30d5\u30a1\u30a4\u30eb\u306b [&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\/5779"}],"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=5779"}],"version-history":[{"count":15,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/5779\/revisions"}],"predecessor-version":[{"id":20382,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/5779\/revisions\/20382"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5779"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}