{"id":6786,"date":"2020-06-15T12:00:57","date_gmt":"2020-06-15T03:00:57","guid":{"rendered":"https:\/\/virtualcast.jp\/blog\/?p=6786"},"modified":"2020-06-12T19:12:28","modified_gmt":"2020-06-12T10:12:28","slug":"nuxt_typescript_asyncdata","status":"publish","type":"post","link":"https:\/\/blog.virtualcast.jp\/blog\/2020\/06\/nuxt_typescript_asyncdata\/","title":{"rendered":"\u5c11\u3057\u8a73\u3057\u304f\u8aad\u307f\u89e3\u3044\u3066\u307f\u308b Nuxt TypeScript \u3067\u306e asyncData \u3068\u305d\u306e\u5468\u8fba"},"content":{"rendered":"<p><img loading=\"lazy\" src=\"https:\/\/virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200604_eyecatch.jpg\" alt=\"\" width=\"1200\" height=\"630\" class=\"alignnone size-full wp-image-6788\" srcset=\"https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200604_eyecatch.jpg 1200w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200604_eyecatch-300x158.jpg 300w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200604_eyecatch-768x403.jpg 768w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200604_eyecatch-1024x538.jpg 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p>\u3053\u3093\u306b\u3061\u306f\u3002UI\/UX\u30c7\u30b6\u30a4\u30f3\u3092\u62c5\u5f53\u3057\u3066\u3044\u308bsadakitchen\u3067\u3059\u3002<br \/>\n\u6700\u8fd1\u306fWeb\u306e\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u3082\u643a\u308f\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30d0\u30fc\u30c1\u30e3\u30eb\u30ad\u30e3\u30b9\u30c8\u306eWeb\u30d5\u30ed\u30f3\u30c8\u3067\u306f Nuxt.js \u3084 vue \u3092\u6d3b\u7528\u3057\u3066\u304a\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u9ad8\u3044\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u66f4\u306b\u3001 TypeScript \u306b\u3088\u3063\u3066\u578b\u5b89\u5168\u6027\u306b\u3088\u308b\u6069\u6075\u3092\u5f97\u308b\u305f\u3081\u306b Nuxt TypeScript \u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\uff08\u53c2\u8003\u8a18\u4e8b\uff1a<a href=\"https:\/\/virtualcast.jp\/blog\/2019\/04\/ux-dx-friendly-with-nuxt-js\/\">nuxt.js \u306b\u3088\u308b UX\/DX \u30d5\u30ec\u30f3\u30c9\u30ea\u30fc\u306a Web \u30b5\u30fc\u30d3\u30b9\u958b\u767a<\/a>\uff09<br \/>\n\u4eca\u56de\u306f Nuxt TypeScript \u3067\u3088\u304f\u4f7f\u3063\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9 asyncData \u3068\u305d\u306e\u5468\u8fba\u306b\u3064\u3044\u3066\u5c11\u3057\u8a73\u3057\u304f\u8aad\u307f\u89e3\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001Nuxt\uff06TypeScript\u306e\u74b0\u5883\u69cb\u7bc9\u306b\u3064\u3044\u3066\u306f\u672c\u8a18\u4e8b\u3067\u306f\u53d6\u308a\u6271\u3044\u307e\u305b\u3093\u3002<br \/>\n\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u8a18\u4e8b\u3092\u53c2\u7167\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n<a href=\"https:\/\/qiita.com\/hiropy0123\/items\/4ee85fdb9b19bc89056f\" target=\"_blank\">3\u5206\u3067\u3064\u304f\u308b2019\u5e74\u7248 Nuxt.js TypeScript \u958b\u767a\u74b0\u5883\u8a2d\u5b9a(Qiita)<\/a><\/p>\n<h2>\u3053\u306e\u8a18\u4e8b\u306e\u5bfe\u8c61\u8005<\/h2>\n<ul>\n<li>Nuxt.js \u3092\u89e6\u3063\u305f\u3053\u3068\u304c\u3042\u308b\u304c\u3001\u6df1\u5800\u308a\u3057\u305f\u3053\u3068\u306f\u306a\u3044\u4eba<\/li>\n<li>TypeScript \u306f\u4f7f\u3063\u3066\u3044\u308b\u304c\u3001Nuxt.js \u306f\u3042\u307e\u308a\u89e6\u3063\u305f\u3053\u3068\u304c\u306a\u3044\u4eba<\/li>\n<\/ul>\n<h2>\u74b0\u5883<\/h2>\n<ul>\n<li>Nuxt v2.12.2\n<ul>\n<li>@nuxt\/typescript-build v0.6.6<\/li>\n<li>@nuxt\/typescript-runtime v0.4.8<\/li>\n<li>nuxt-property-decorator v2.7.2<\/li>\n<li>@nuxt\/http v0.5.1<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h2>\n<p>SSR (universal) \u30e2\u30fc\u30c9\u306e Nuxt \u306e\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3001`asyncData` \u30e1\u30bd\u30c3\u30c9\u304b\u3089 API \u3092\u53e9\u3044\u3066\u3001\u305d\u306e\u7d50\u679c\u3092\u8fd4\u3059\u3088\u3046\u306a\u3082\u306e\u3092\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u6b21\u306e\u30b3\u30fc\u30c9\u306f `HogePage` \u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u30af\u30a8\u30ea `id` \u3092\u542b\u3093\u3067\u8868\u793a\u3059\u308b\u3068\u3001 json \u30c7\u30fc\u30bf\u3092\u8fd4\u3059 API \u3092\u53e9\u304d\u3001string\u304c\u5909\u3048\u308b\u60f3\u5b9a\u306e\u4f5c\u308a\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-pages\/HogePage.vue\">\/\/ (\u524d\u7565)\r\n<script lang=\"ts\">\r\nimport { Component, Vue } from 'nuxt-property-decorator'\r\nimport { Route } from 'vue-router'\r\nimport { NuxtHTTPInstance } from '@nuxt\/http'\r\n@Component\r\nexport default class HogePage extends Vue {\r\n  async asyncData({\r\n    query,\r\n    $http,\r\n  }: {\r\n    query: Route['query']\r\n    $http: NuxtHTTPInstance\r\n  }): Promise<{ title: string }> {\r\n    const res: { title: string } = await $http.$get(\r\n      `http:\/\/localhost:3000\/api\/news?post=${query.id}`\r\n    )\r\n    return { title: res.title }\r\n  }\r\n}\r\n<\/script>\r\n\/\/ (\u5f8c\u7565)<\/code><\/pre>\n<h2>\u6df1\u5800\u308a\u3057\u3066\u307f\u308b<\/h2>\n<p>\u3067\u306f\u5404\u30b3\u30fc\u30c9\u6df1\u5800\u308a\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre><code><script lang=\"ts\"><\/code><\/pre>\n<p>TypeScript\u3092\u4f7f\u3046\u969b\u306b <code>lang=\"ts\"<\/code> \u3068\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u5909\u6570\u3092\u6271\u3046\u969b\u306b string \u3084 number \u3068\u3044\u3063\u305f\u300c\u578b\u300d\u3092\u660e\u793a\u7684\u306b\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u30a8\u30e9\u30fc\u30c1\u30a7\u30c3\u30af\u304c\u3088\u308a\u53b3\u683c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code>import { Component, Vue } from 'nuxt-property-decorator'<\/code><\/pre>\n<p>`nuxt-property-decorator` \u3068\u306f\u3001 vue-property-decorator \u306b\u52a0\u3048\u3001 Nuxt \u306e\u72ec\u81ea\u306e\u30e1\u30bd\u30c3\u30c9\u3084\u30d7\u30ed\u30d1\u30c6\u30a3\u306a\u3069\u3082\u30c7\u30b3\u30ec\u30fc\u30c8\u3057\u3066\u304f\u308c\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3059\u3002<br \/>\n\u3053\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30af\u30e9\u30b9\u30d9\u30fc\u30b9\u3067\u306e\u8a18\u8ff0\u304c\u53ef\u80fd\u306b\u306a\u308a\u3001`Component`\u3084`Prop`\u306a\u3069\u3092\u30af\u30e9\u30b9\u306e\u69cb\u6210\u8981\u7d20\uff08\u30af\u30e9\u30b9\u30e1\u30f3\u30d0\uff09\u3068\u3057\u3066\u3001`@Component`\u3084`@Prop`\u3068\u3044\u3046\u5f62\u3067\u898b\u901a\u3057\u3088\u304f\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08\u30af\u30e9\u30b9\u306b\u5bfe\u3057\u3066\u76f4\u63a5\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u5b9a\u7fa9\u3067\u304d\u308b\uff09<br \/>\nnuxt-property-decorator \u306e\u4f9d\u5b58\u95a2\u4fc2\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_1.jpg\" alt=\"\" width=\"1200\" height=\"1090\" class=\"alignnone size-full wp-image-6812\" srcset=\"https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_1.jpg 1200w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_1-300x273.jpg 300w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_1-768x698.jpg 768w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_1-1024x930.jpg 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<div class=\"column\">\n<h3>\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3068\u306f\uff1f<\/h3>\n<p>\u30af\u30e9\u30b9\u5ba3\u8a00\u3084\u30e1\u30bd\u30c3\u30c9\u3001\u30a2\u30af\u30bb\u30b5\u3001\u30d7\u30ed\u30d1\u30c6\u30a3\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306b\u7d50\u3073\u4ed8\u3051\u3089\u308c\u308b\u7279\u5225\u306a\u5ba3\u8a00\u306e\u4e00\u7a2e<br \/>\n\u3068\u8aac\u660e\u3055\u308c\u307e\u3059\u304c\u3001\u3082\u3046\u5c11\u3057\u308f\u304b\u308a\u3084\u3059\u304f\u3059\u308b\u3068\u3001<br \/>\n\u300c\u3042\u308b\u30b3\u30fc\u30c9\u3092\u4ed6\u306e\u30b3\u30fc\u30c9\u3067\u5185\u5305\u3057\u3064\u3064\u5b9f\u884c\u3059\u308b\u3082\u306e\u300d\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001<br \/>\n\u3042\u308b\u30b3\u30fc\u30c9\u304c <code>Hello,world.<\/code> \u3092\u8fd4\u3059\u5834\u5408\u3001<br \/>\n\u4ed6\u306e\u30b3\u30fc\u30c9\u3067\u5185\u5305\u3057\u3066\u3044\u308b\u3068\u3001<code>Hello,world.<\/code> \u3092\u542b\u3093\u3067\u8fd4\u3059\u5b9f\u88c5\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>JavaScript\u3067\u306f\u300c@\u300d\u3092\u3064\u3051\u3066\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u8868\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001 Nuxt TypeScript \u3067\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3092\u6271\u3046\u5834\u5408\u306f tsconfig.json \u306e compilerOptions \u306b<br \/>\n<code>\"experimentalDecorators\": true,<\/code> \u3068\u8a18\u8ff0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3001\u975e\u5e38\u306b\u8aac\u660e\u304c\u81a8\u5927\u306a\u305f\u3081\u3001\u3053\u306e\u8a18\u4e8b\u3067\u306f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306e\u5b9f\u88c5\u306b\u95a2\u308f\u308b\u90e8\u5206\u307e\u3067\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<\/div>\n<pre><code>import { Route } from 'vue-router'\r\nimport { NuxtHTTPInstance } from '@nuxt\/http'<\/code><\/pre>\n<p>asyncData \u306e \u5f15\u6570 query \u3068 $http \u306e\u578b\u3092\u5b9a\u7fa9\u3059\u308b\u305f\u3081\u306b\u8aad\u307f\u8fbc\u3093\u3067\u3044\u307e\u3059\u3002<\/p>\n<pre><code>@Component\r\nexport default class HogePage extends Vue {<\/code><\/pre>\n<p>Component \u3092 \u30af\u30e9\u30b9\u5ba3\u8a00 \u3059\u308b\u305f\u3081\u306b\u3001\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3092\u5229\u7528\u3059\u308b\u8a18\u8ff0\u3067\u3059\u3002<br \/>\n\u3053\u306e\u8a18\u8ff0\u306b\u3088\u308a HogePage \u30af\u30e9\u30b9\u3092 Vue\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code>  async asyncData({\r\n    query,\r\n    $http,\r\n  }: {\r\n    query: Route['query']\r\n    $http: NuxtHTTPInstance\r\n  }): Promise<{ title: string }> {<\/code><\/pre>\n<p>\u4eca\u56deAPI\u3092\u53e9\u304f\u3053\u3068\u3067\u3001\u975e\u540c\u671f\u30c7\u30fc\u30bf\u3092\u6271\u3046\u3053\u3068\u306b\u306a\u308b\u305f\u3081\u3001 <code>async<\/code> \u3092\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nasyncData\u5185\u90e8\u306b\u306f <code>await<\/code> \u304c\u5165\u308a\u307e\u3059\u3002<\/p>\n<p>asyncData\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u306f context \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5165\u308b\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u4e2d\u304b\u3089\u4f7f\u7528\u3059\u308b\u30ad\u30fc\u3060\u3051\u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306f\u30af\u30a8\u30ea\u306e <code>id<\/code> \u3092\u53d6\u5f97\u3057\u305f\u3044\u305f\u3081\u3001 <code>route.query<\/code> \u306e\u30a8\u30a4\u30ea\u30a2\u30b9\u3068\u306a\u308b <code>query<\/code> \u3001\u975e\u540c\u671f\u51e6\u7406\u3092\u884c\u3046\u305f\u3081\u306eHTTP\u30e2\u30b8\u30e5\u30fc\u30eb <code>$http<\/code> \u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u8fd4\u308a\u5024\u306f\u975e\u540c\u671f\u51e6\u7406\u3068\u306a\u308b\u305f\u3081\u3001Promise\u3067\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"column\">\n<h3>asyncData\u3068\u306f\uff1f<\/h3>\n<p>asyncData \u306f\u30b5\u30fc\u30d0\u30fc\u304b\u3089\u306e\u30c7\u30fc\u30bf\u3092\u975e\u540c\u671f\u3067\u53d6\u5f97\u3057\u3001 Nuxt.js \u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067\u6271\u3046\u6e96\u5099\u3092\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u306e\u307f\u5b9f\u884c\u3055\u308c\u308b<\/li>\n<li>\u30da\u30fc\u30b8\u8868\u793a\u6642\u306b\u6bce\u56de\u5b9f\u884c\u3055\u308c\u308b(Nuxt.js\u306e\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u30a4\u30d9\u30f3\u30c8)<\/li>\n<li>\u7b2c\u4e00\u5f15\u6570\u306b context \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5165\u308b<\/li>\n<li>asyncData\u5185\u3067 this \u306f\u4f7f\u3048\u306a\u3044\n<ul>\n<li>asyncData\u5b9f\u884c\u6642\u306b\u306f\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u751f\u6210\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\n<ul>\n<li><a href=\"https:\/\/ja.nuxtjs.org\/blog\/understanding-how-fetch-works-in-nuxt-2-12\/\" target=\"_blank\">Nuxt2.12\u3067fetch\u304c\u5168\u3066\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u4f7f\u3048\u3001this\u3082\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u3063\u305f<\/a>\u306e\u3067\u305d\u3063\u3061\u3092\u4f7f\u3046\u306e\u3082\u3042\u308a\u304b\u3082<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>asyncData\u306e\u8fd4\u308a\u5024\u306f data \u3068\u7d71\u5408\u3055\u308c\u308b<\/li>\n<li>\u975e\u540c\u671f\u306e\u5834\u5408\u3001asyncData\u306e\u8fd4\u308a\u5024\u306f Promise<T> \u3068\u306a\u308b<\/li>\n<\/ul>\n<p>\u307e\u305fasyncData\u304cNuxt\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u5185\u3067\u5b9f\u884c\u3055\u308c\u308b\u9806\u756a\uff08\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\uff09\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u8a73\u3057\u304f\u306f<a href=\"https:\/\/qiita.com\/japboy\/items\/b67bae0bac1aeefb2680#the-big-picture\" target=\"_blank\" >Vue.js \u3068 Nuxt.js \u306e\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u65e9\u5f15\u304d\u30e1\u30e2(Qiita)<\/a>\u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_2-1.jpg\" alt=\"\" width=\"1200\" height=\"1090\" class=\"alignnone size-full wp-image-6817\" srcset=\"https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_2-1.jpg 1200w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_2-1-300x273.jpg 300w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_2-1-768x698.jpg 768w, https:\/\/blog.virtualcast.jp\/blog\/wp-content\/uploads\/2020\/06\/200608_figure_2-1-1024x930.jpg 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/>\n<\/div>\n<div class=\"column\">\n<h3>context\u3068\u306f<\/h3>\n<p>\u7279\u5b9a\u306e\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u3067\u4f7f\u7528\u3067\u304d\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002<br \/>\n\u69d8\u3005\u306a\u30ad\u30fc\u304c\u5165\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b5\u30a4\u30c9\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u5171\u901a<\/h4>\n<ul>\n<li>app<\/li>\n<li>store<\/li>\n<li>route<\/li>\n<li>params<\/li>\n<li>query<\/li>\n<li>env<\/li>\n<li>isDev<\/li>\n<li>isHMR<\/li>\n<li>redirect<\/li>\n<li>error<\/li>\n<\/ul>\n<h4>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b5\u30a4\u30c9\u306e\u307f<\/h4>\n<ul>\n<li>from<\/li>\n<li>nuxtState<\/li>\n<\/ul>\n<h4>\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306e\u307f<\/h4>\n<ul>\n<li>req<\/li>\n<li>res<\/li>\n<li>beforeNuxtRender<\/li>\n<\/ul>\n<p>\u306a\u304a\u3001Nuxt.js\u306e\u30d7\u30e9\u30b0\u30a4\u30f3\u306b\u3088\u3063\u3066\u306f\u3001 <code>$http<\/code> \u3084 <code>$i18n<\/code> \u3068\u3044\u3046\u3088\u3046\u306b <code>$<\/code> \u3092\u3064\u3051\u3066\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<\/div>\n<pre><code>    const res: { title: string } = await $http.$get(\r\n      `http:\/\/localhost:3000\/api\/news?post=${query.id}`\r\n    )\r\n    return { title: res.title }<\/code><\/pre>\n<p>HTTP\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5229\u7528\u3057\u3066\u3001API\u3092\u53e9\u304d\u3001\u8fd4\u3063\u3066\u304d\u305f title \u30ad\u30fc\u3092\u542b\u3080 json \u5f62\u5f0f\u306e\u4e2d\u304b\u3089 title \u30c7\u30fc\u30bf\u3092\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nAPI \u3092\u53e9\u304f\u969b\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306f http \u3067\u306f\u306a\u304f\u3001 axios \u3067\u3082\u69cb\u3044\u307e\u305b\u3093\u3002<br \/>\n\u306a\u304a HTTP\u30e2\u30b8\u30e5\u30fc\u30eb \u306b\u306f axios \u3068\u6bd4\u8f03\u3057\u3066\u3053\u306e\u3088\u3046\u306a\u9055\u3044\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u8efd\u91cf<\/li>\n<li>fetch API \u3092\u5229\u7528\u3059\u308b\u306e\u3067 service worker \u3068\u306e\u89aa\u548c\u6027\u304c\u5411\u4e0a<\/li>\n<li>node.js \u5074\u306f node-fetch \u3092\u7528\u3044\u3066\u307b\u307c\u30ed\u30b8\u30c3\u30af\u306a\u3057\u3067\u5bfe\u5fdc<\/li>\n<\/ul>\n<p>(\u53c2\u8003\u8a18\u4e8b <a href=\"https:\/\/qiita.com\/il-m-yamagishi\/items\/a823c1afbdf253e84a34\" target=\"_blank\">nuxt.js \u306f axios \u3092\u6368\u3066\u3066 ky \u306b\u306a\u308b\u69d8\u5b50(Qiita)<\/a> )<\/p>\n<p>IE\u3092\u5bfe\u8c61\u3068\u3057\u306a\u3044\u30b5\u30a4\u30c8\u306e\u5834\u5408\u306f\u3001HTTP\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3046\u306e\u304c\u3088\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<div class=\"column\">\n<h3>proxy\u306b\u3064\u3044\u3066\uff1f<\/h3>\n<p>\u306a\u304a\u4f59\u8ac7\u3067\u3059\u304c\u3001\u5916\u90e8\u30c9\u30e1\u30a4\u30f3\u306eAPI\u3092\u53e9\u3044\u305f\u969b\u306b CORS \u306b\u95a2\u3059\u308b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u305d\u306e\u969b\u306f <code>@nuxtjs\/proxy<\/code> \u3067\u30d7\u30ed\u30af\u30b7\u3092\u4ecb\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u3067\u30a8\u30e9\u30fc\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-pages\/nuxt.config.js\">    \/\/ (\u524d\u7565)\r\n    \/*\r\n     ** Nuxt.js modules\r\n     *\/\r\n    modules: ['@nuxtjs\/style-resources', '@nuxt\/http', '@nuxtjs\/proxy'], \/\/ \u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u767b\u9332\r\n    http: {\r\n      proxy: true, \/\/ proxy \u3092\u4f7f\u3046\r\n    },\r\n    proxy: {\r\n      '\/api\/': {\r\n        target: 'http:\/\/api.example.com', \/\/ \u5b9f\u969b\u306eAPI\u306eURL\r\n        pathRewrite: { '^\/api\/': '' }, \/\/ HTTP\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u53e9\u3044\u305fURL\u306e `\/api` \u3092\u524a\u9664\r\n      },\r\n    },\r\n    \/\/ (\u5f8c\u7565)<\/code><\/pre>\n<p>\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001nuxt generate\u3067\u9759\u7684\u306a\u30a6\u30a7\u30d6\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u3057\u3066\u30d3\u30eb\u30c9\u3057\u305f\u969b\u306b\u306f\u4f7f\u7528\u3067\u304d\u307e\u305b\u3093\u3002<br \/>\n\u672c\u756a\u3067\u306f\u304d\u3061\u3093\u3068\u30b5\u30fc\u30d0\u30fc\u5074\u3067 CORS \u306e\u8a2d\u5b9a\u3092\u884c\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<\/div>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u4ee5\u4e0a\u306e\u3088\u3046\u306b asyncData \u3068\u305d\u306e\u5468\u8fba\u3092\u8aad\u307f\u89e3\u3044\u3066\u898b\u307e\u3057\u305f\u3002<br \/>\n\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u3068\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30c7\u30b3\u30ec\u30fc\u30bf <code>nuxt-property-decorator<\/code> \u3092\u4f7f\u3046\u3053\u3068\u3067Vue\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u62e1\u5f35\u3067\u304d\u308b<\/li>\n<li>asyncData\u306e\u5f15\u6570 context \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306f Vue \u306b\u95a2\u3059\u308b\u4fbf\u5229\u306a\u30ad\u30fc\u304c\u5165\u3063\u3066\u3044\u308b<\/li>\n<li>asyncData\u306e\u8fd4\u308a\u5024\u306f Vue \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e data \u3068\u7d71\u5408\u3055\u308c\u308b<\/li>\n<li>CORS\u306e\u5834\u5408\u306f <code>@nuxtjs\/proxy<\/code> \u3092\u4f7f\u3046<\/li>\n<\/ul>\n<p>Nuxt.js \u306f\u975e\u5e38\u306b\u5f37\u529b\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u4f7f\u3044\u3053\u306a\u3059\u306e\u304c\u3068\u3066\u3082\u5927\u5909\u3067\u3059\u3002<br \/>\n\u304c\u3001\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u307f\u3064\u3064\u3001\u30b3\u30fc\u30c9\u3092\u76f4\u63a5\u898b\u3066\u3044\u304f\u3068\u3001\u6c17\u3065\u304b\u306a\u304b\u3063\u305f\u632f\u308b\u821e\u3044\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3068\u304a\u3082\u3044\u307e\u3059\u3002<br \/>\n\u4e00\u3064\u4e00\u3064\u5185\u5bb9\u3092\u8aad\u307f\u8fbc\u307f\u3001\u632f\u308b\u821e\u3044\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u4f7f\u3044\u3053\u306a\u305b\u308b\u7b2c\u4e00\u6b69\u306b\u306a\u308b\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002UI\/UX\u30c7\u30b6\u30a4\u30f3\u3092\u62c5\u5f53\u3057\u3066\u3044\u308bsadakitchen\u3067\u3059\u3002 \u6700\u8fd1\u306fWeb\u306e\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u3082\u643a\u308f\u3063\u3066\u3044\u307e\u3059\u3002 \u30d0\u30fc\u30c1\u30e3\u30eb\u30ad\u30e3\u30b9\u30c8\u306eWeb\u30d5\u30ed\u30f3\u30c8\u3067\u306f Nuxt.js \u3084 vue \u3092\u6d3b\u7528\u3057\u3066\u304a\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9...<\/p>\n","protected":false},"author":19,"featured_media":6788,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[150,26],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/posts\/6786"}],"collection":[{"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/comments?post=6786"}],"version-history":[{"count":36,"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/posts\/6786\/revisions"}],"predecessor-version":[{"id":6858,"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/posts\/6786\/revisions\/6858"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/media\/6788"}],"wp:attachment":[{"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/media?parent=6786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/categories?post=6786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.virtualcast.jp\/blog\/wp-json\/wp\/v2\/tags?post=6786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}