ã¯ããã«
ãNuxt.jsã¯ãWebãã¼ã¸ã®ã¦ã¼ã¶ã¼ã¤ã³ã¿ãã§ã¼ã¹ï¼UIï¼ãã¬ã¼ã ã¯ã¼ã¯ã§ããVue.jsã«ãWebãã¼ã¸ã®ä½æã«å¿ è¦ãªUI以å¤ã®è¿½å æ©è½ãã¾ã¨ãã¦æä¾ãããã¬ã¼ã ã¯ã¼ã¯ã§ãã
ãWebãã¼ã¸ãè¤æ°ã®ã³ã³ãã¼ãã³ãã«åå²ã§ããJavaScriptãã¬ã¼ã ã¯ã¼ã¯ã§ã¯ãããã³ã³ãã¼ãã³ãã®ç¶æ ï¼å ¥åï¼è¡¨ç¤ºå 容ï¼ãä»ã®ã³ã³ãã¼ãã³ãã§å©ç¨ããå ´åãä¸æ´åãªãç¶æ ãå ±æããä»çµã¿ãå¿ è¦ã¨ãªãã¾ãã
ããããã£ãä»çµã¿ã¨ãã¦ãWebãã¼ã¸å ¨ä½ã®ç¶æ ãåä¸ã®å ´æã«ä¿æãã¦ãåã³ã³ãã¼ãã³ãããåç §ï¼æ´æ°ããæ¹æ³ãææ¡ããã¾ãããããããæ¹æ³ã¯ãReactã§å©ç¨ãããFluxãReduxãæåã§ãããVue.jsã§ã¯Vuexã¨å¼ã°ããä»çµã¿ãå©ç¨ã§ãã¾ããNuxt.jsã®ããã¸ã§ã¯ãã§ã¯ãVuexã§ç¶æ ãä¿æãããVuexã¹ãã¢ãããæ¨æºã§ï¼è¿½å ã¤ã³ã¹ãã¼ã«ãªãã§ï¼å©ç¨ã§ãã¾ãã
ãæ¬è¨äºã§ã¯ãNuxt.jsã®Webãã¼ã¸ã§ãVuexã¹ãã¢ãå©ç¨ãã¦ç¶æ ã管çããæ¹æ³ãããµã³ãã«ã¨ã¨ãã«èª¬æãã¦ããã¾ãã
対象èªè
- Vuexã®èãæ¹ãç解ãããæ¹
- Nuxt.jsã§ã³ã³ãã¼ãã³ãéã®ãã¼ã¿é£æºãã·ã³ãã«ã«è¡ãããæ¹
- è¤éãªãã¼ã¿æ§é ãæã¤Webãã¼ã¸ãä½ãããæ¹
å¿ è¦ãªç°å¢
ãæ¬è¨äºã®ãµã³ãã«ã³ã¼ãã¯ã以ä¸ã®ç°å¢ã§åä½ã確èªãã¦ãã¾ãã
-
Windows 10 64bitç
- Node.js v12.13.1 64bitç
- Nuxt.js 2.10.2
- Vue.js 2.6.10
- Microsoft Edge 44.18362.449.0
ããµã³ãã«ã³ã¼ãã¯ãNuxt.jsã®CLIãã¼ã«ï¼create-nuxt-appï¼ã§çæããããã¸ã§ã¯ããå ã«å®è£ ãã¦ãã¾ããCLIãã¼ã«ã®å©ç¨æ³ãããã¸ã§ã¯ãæ§æãªã©ã®è©³ç´°ã¯ãé£è¼ç¬¬1åã®è¨äºãåç §ãã¦ãã ããã
ããµã³ãã«ã³ã¼ããå®è¡ããã«ã¯ããµã³ãã«ã®ãã©ã«ãã¼ã§ãnpm installãã³ãã³ããå®è¡ãã¦ã©ã¤ãã©ãªã¼ããã¦ã³ãã¼ãå¾ããnpm run devãã³ãã³ããå®è¡ãã¦ãWebãã©ã¦ã¶ã¼ã§ãhttp://localhost:3000/ããéãã¾ãã
Vuexã®æ§æè¦ç´ ã¨å¦çã®æµã
ãæåã«ãVuexãæ§æããè¦ç´ ã¨å¦çã®æµãããå³1ã§èª¬æãã¾ããVuexã¹ãã¢ã¯ãå³ã®éè²ç¹ç·é¨åã®æ©è½ãæä¾ãã¾ãã
ãã³ã³ãã¼ãã³ããWebãã¼ã¸ã®ç¶æ ãæ´æ°ããã«ã¯ãVuexã¹ãã¢ã®ãã¢ã¯ã·ã§ã³ãï¼1ï¼ãå®è¡ãã¾ãï¼ã¢ã¯ã·ã§ã³ã®å®è¡ããDispatchãã¨å¼ã³ã¾ãï¼ãã¢ã¯ã·ã§ã³ã§ã¯ãå¿ è¦ã«å¿ãã¦Web APIãªã©ãå®è¡ãã¦ãæ´æ°ãããã¼ã¿ãåå¾ãã¾ãã
ãã¢ã¯ã·ã§ã³ã§åå¾ãããã¼ã¿ãç¶æ ã«åæ ããã«ã¯ãããã¥ã¼ãã¼ã·ã§ã³ãï¼2ï¼ãå®è¡ãã¾ãï¼ãã¥ã¼ãã¼ã·ã§ã³ã®å®è¡ããCommitãã¨å¼ã³ã¾ãï¼ãVuexã§ã¯ãéåæå¦çã¨ç¶æ ã®æ´æ°ãåé¢ãã¦æ±ããããWeb APIå®è¡ãªã©ã®éåæå¦çã¯ã¢ã¯ã·ã§ã³ã§è¡ãããã¥ã¼ãã¼ã·ã§ã³ã¯å¿ ãåæçã«è¡ãã¾ãã
ãï¼3ï¼ã®ãã¹ãã¼ããã¯ãWebãã¼ã¸å ¨ä½ã®ç¶æ ãä¿æãã¾ãããã¥ã¼ãã¼ã·ã§ã³ã¯ã¹ãã¼ãã®å¤æ´ï¼Mutateï¼ãè¡ããã³ã³ãã¼ãã³ãã¯å¤æ´ãããã¹ãã¼ãã«åºã¥ãã¦ç»é¢ã表示ï¼Renderï¼ãã¾ãã
ããã®æµãã«åºã¥ãã¦Vuexã¹ãã¢ã«ç¶æ ãæ ¼ç´ãããã¨ã§ãéçºè ã¯ãWebãã¼ã¸ã®ç¶æ 管çãVuexã¹ãã¢ã«ä»»ãããã¨ãã§ãã¾ãã