ããã«ã¡ã¯ãèªæèéå°ãªæ£ç¾©ã®ãã¼ãã¼ã§ã馴æã¿ã®æ ªå¼ä¼ç¤¾SCOUTERã®ç³å²¡ å°æ( @masaakikunsan )ã§ãã
ååã® Flutter ã®ããã°ã§ããï¼æåã°ã«APIã®å©ãæ¹ããã¼ã¸å ã®ã¬ã¤ã¢ã¦ãä½ãæ¹ãªã©ãã¡ããã¨è§£èª¬ããããã°ãæ¸ããã¨æãã®ã§ã楽ãã¿ã«ï¼ãã¨è¨ã£ãã®ã§ããããã以é Flutter ã触ãã¦ããªãã®ã§ä»åã¯æ®æ®µè§¦ã£ã¦ãæè¡ã«ã¤ãã¦æ¸ãã¾ãã
ç¾å¨ SCOUTER ã§ã¯ãSARDINE 㨠back check ã®å¤§ããåãã¦ï¼ã¤ã®éçºãã¼ã ã§éçºãè¡ã£ã¦ãã¾ãã åã¯ãback check 㧠ãã¬ã¤ã³ã°ããã¼ã¸ã£ã¼ã¨ãã¦éçºããã¦ããã®ã§ãããSARDINE ãã¼ã ã«æè¿ããã³ãã¨ã³ãã®ç¸è«ãåããããã«ãªã£ã¦ããã®ã§ä»åã¯ãã®è©±ããã¦ããã¾ãã
Vuex ã§ã®ã«ã¼ã«
Vue ã Nuxt ã§éçºãã¦ããå ´åç¶æ 管çã« Vuex ã使ã£ã¦ãããã¨æãã¾ãã ãªã®ã§ãä»å㯠Vuex ã®æ¦å¿µãæ¸ãæ¹ã«ã¤ãã¦ã¯çç¥ãåãéçºããã¨ãã®ã«ã¼ã«ãç´¹ä»ãã¾ãã
namespaced ã true ã«ããã
namespaced ã true
ã«ãããã¨ã§ãã¢ã¸ã¥ã¼ã«ãã¨ã«èªåçã«åå空éãä»ä¸ããã¾ãã
ããããªãã¨ãè¤æ°ã®ã¢ã¸ã¥ã¼ã«ãåããã¥ã¼ãã¼ã·ã§ã³/ã¢ã¯ã·ã§ã³ã¿ã¤ãã«åå¿ãããã¨ãã§ãã¦ãã¾ã大ãããªã£ãã¨ãã«ç ´ç¶»ãã¦ãã¾ãã§ãããã
Nuxt ã§éçºããã¦ããå ´åãã¢ã¸ã¥ã¼ã«ã¢ã¼ããä¸è¨ã® namespaced=true ã«ãªãã¾ãã
mapState ã使ããªã
Vuex ã«ã¯ mapGetters ãã«ãã¼ãåå¨ãã¾ãã mapGetters ã¯ã²ãã¿ã¼ã®è©ä¾¡å¾ã®å¤ãè¿ãã³ã³ãã¼ãã³ãã® computed ãªãã·ã§ã³ãä½æãã¾ãã Vuex ã® state ã使ãããæã« filter ãããããªãããå¤æ´ãããã¨ããã¢ããã¼ã·ã§ã³ã¯çµæ§ããã¨æãã¾ãã ãã®ãããªçç±ãã getters ã§ã¯ state ãå å·¥ããªããã¼ã¿ãå ¨ã¦ä¹ã mapGetters ã§çµ±ä¸ããã¨ãããããããªã£ã¦è¯ãã§ãããã
mapMutations ã使ããªã
state ãå¤æ´ããå¯ä¸ã®æ¹æ³ã¯ãã¥ã¼ãã¼ã·ã§ã³ã§ãã ãªã®ã§ã³ã³ãã¼ãã³ãå ã§ç´æ¥ mapMutations ã使ã£ã¦ãã¥ã¼ãã¼ã·ã§ã³ãã³ãããããæ¹ãçµæ§ãã¾ããã actions ã§ãã¥ã¼ãã¼ã·ã§ã³ãã³ãããããã®ãè¯ãã§ãããã ãããããã¨ã§ã³ã³ãã¼ãã³ãå 㧠mapActions ãã«ãã¼ã使ãã ãã§ãããªããããããããªãã¾ããã管çã楽ã«ãªãã¾ãã
å³æ ¼ã¢ã¼ãã«ããã
æ®éã«ã«ã¼ã«ã«ãããã£ã¦ãã£ã¦ããããã¯æ¬æ¥ããªãã¦ããã®ã§ãããçµæ§ããããªããã¸ã§ã¯ã㧠state ããã¥ã¼ãã¼ã·ã§ã³ãã³ãã©ã®å¤é¨ã§å¤æ´ãã¦ãã³ã¼ããè¦ã¾ãã ãªã®ã§ãããããå³æ ¼ã¢ã¼ãã«ããã¥ã¼ãã¼ã·ã§ã³ãã³ãã©å¤é¨ã§ state ãå¤æ´ã§ããªãããã«ãã¾ãããã
Nuxt ã§éçºãã¦ããå ´åããã©ã«ãã§å³æ ¼ã¢ã¼ãã«ãªã£ã¦ããã®ã§ç¹ã«è¨å®ãªã©ã¯ããã¾ããã
state ãå ¨ã¦ Vuex ã«éãã®ããããã
çµæ§ Redux ãæ¸ãã¦ãã人㯠state ãå ¨ã¦ Vuex ã«éãã¦ããã®ã§ãããã㯠store ãè¥å¤§åãããå©é·ã§ã¾ãããã©ããªããããã¨ã¯ããã¾ããã ç¶æ ãã³ã³ãã¼ãã³ãå ã§å®çµããã®ã§ããã°ããã¯ãã¼ã«ã«ã¹ãã¼ãã«æããã¾ãããã
ã¢ã¸ã¥ã¼ã«ã®åãæ¹
以åã¯ãã¼ã¸æ¯ Vuex ã®ãã¡ã¤ã«ãä½æãã¦ããã®ã§ãããããããã¨è¤æ°ãã¼ã¸ã§å ±éãã state ãã§ã¦ãã¦å°ãã¾ããã ä»ã¯ãã¡ã¤ã³ãã¨ã«ãã¡ã¤ã«ãä½æããããã«ãã¦ãã¾ãã ãããããã¨ã§ãéãã¢ã¸ã¥ã¼ã«ã® state ããããå¿ è¦æ§ã¯åºæ¬çã«ãªãã§ãããã
以ä¸ãåã Vuex ãæ¸ãä¸ã§ã®ã«ã¼ã«ã§ãã 次㫠Component ã®è¨è¨ã«ã¤ãã¦æ¸ãã¦ããã¾ãã
Component ã®è¨è¨
Component ãè¨è¨ããä¸ã§ããè³ã«ããã®ããåå©ç¨æ§ã®é«ããã§ãã åã¯ãComponent ãè¨è¨ããä¸ã§åå©ç¨æ§ã¯ãã¾ãèããªãããã«ãã¦ã¾ãã ã¾ãã¯ããã«ãã®çç±ã«ã¤ãã¦æ¸ãã¾ãã
åå©ç¨æ§ã®é«ãã³ã³ãã¼ãã³ã
åå©ç¨æ§ã®é«ãã³ã³ãã¼ãã³ããä½ãä¸ã§ã3ã¤ã®ãã¨ãå¿ è¦ã ã¨åã¯æã£ã¦ãã¾ãã
- å®è£ è ã®è½å
- ãã¶ã¤ãã¼ã®è½å
- æé
1ã«é¢ãã¦ã¯è¨ãããããªã ã¨ã¯æãã¾ãããå®è£ è ã®è½åããªãå ´åä¾åæ§ãé«ãã£ããçåå©ç¨æ§ã®ãã¾ãé«ããªãã³ã³ãã¼ãã³ãã«ãªã£ã¦ãã¾ããã¨ãå¤ãã§ãããã
2ã¯ä»¥å¤ã¨é ã«ãªã人ãå¤ãã®ã§ãããããã大äºã§ãã ãã¶ã¤ãã¼ã¯ã³ã³ãã¼ãã³ãå¿åãã¾ãç解ãã¦ããå¿ è¦ãããã¾ãã次ã«éå»ã»æªæ¥ãèãåå©ç¨æ§ãã§ãããã®ã¯åå©ç¨ãã§ããããã«è¨è¨ããã¶ã¤ã³ãããªããã°ããã¾ããã
ãã¶ã¤ãã¼ãå®è£ ãã§ãã人éã§ããã°ãããã¯å½ç¶é ã«ããã¨æãã®ã§åé¡ãªãã¨æãã®ã§ãããå¿ ãããä¸è¨ããã¶ã¤ãã¼ãã§ããå¿ è¦ãããããã§ã¯ããã¾ããã ã¨ã³ã¸ãã¢ã¨åæ¥ãããã£ããã¨ã³ãã¥ãã±ã¼ã·ã§ã³ãã¨ãããã°ä¸è¨ãæºãããã¶ã¤ã³ãã§ããã§ãããã
3ã®æéã¯å½ããåã ãã¨ã¯æãã¨æãã®ã§ãããå£ã§è¨ãã»ã©åå©ç¨æ§ã®é«ãã³ã³ãã¼ãã³ãã¯ç°¡åã«ã¯ä½ããªãã®ã§æã£ã¦ã以ä¸ã«æéã¯ããããã¨ãã話ã§ãã åå©ç¨æ§ã®é«ãã³ã³ãã¼ãã³ããä½ãä¸ã§ãã³ã³ãã¼ãã³ãã®ç²åº¦ã»ãã¸ãã¯ã®ã·ã³ãã«ãçè²ã ã¨èãããã¨ãããã¾ãã ãªã®ã§æéã¯ããããã®ã¨èããã®ãè¯ãã§ãããã
åå©ç¨æ§ã®é«ãã³ã³ãã¼ãã³ããä½ãé£ããã¯ããã¾ã§ã§ããã£ã¦ããã ããã¨æãã¾ãã æ¬é¡ã®åå©ç¨æ§ããã¾ãèããªãã¦ããã¨èãã¦ããçç±ã§ããã大ããçç±ã¨ãã¦ã¯ã©ãã使ããªãã¨ããã®ãããã¾ãã
ãããã¯ãéçºããã¦ãã人ã§ããã°ãå²ã¨çµé¨ããã¨æãã®ã§ãããåå©ç¨æ§ãèãã¦ã³ã³ãã¼ãã³ããä½ãè¾¼ãã ã¯è¯ãããã®å¾ãã¶ã¤ã³ã§ä½¿ããããã¨ããªãã£ã¦ã®ã¯ããã«ããã¾ãã ãããããã¨ãèããã¨åå©ç¨æ§ã®é«ãã³ã³ãã¼ãã³ããä½ãé£ããã¨ã³ã¹ããèããã¨ãã¾ãåå©ç¨æ§ã¯ãã¾ãæèããªãã»ããè¯ãææ決å®ã§ãããã
åã¯2åãã3ååããã¶ã¤ã³ãã§ã¦ãããåãé¢ãã³ã³ãã¼ãã³ãåããããã«ãã¦ãã¾ãã
次ã«ã³ã³ãã¼ãã³ããä½ãã¨ãã«èãããã¨ãæ¸ãã¦ããã¾ãã
ã³ã³ãã¼ãã³ããä½ãä¸ã§èãã¦ãããã¨
åã¯ãã³ã³ãã¼ãã³ãä½ãä¸ã§ä»¥ä¸ï¼ã¤ãæèãã¦ãã¾ãã
- ã³ã³ãã¼ãã³ãã®å½¹å²ãèãã
- ä¾åãå°ãªããã
- ãªãã¹ãç¶æ ãæããªãããã«ãã
ã³ã³ãã¼ãã³ãã®å½¹å²ãèãã
ã³ã³ãã¼ãã³ãå¿åã§éçºããã¦ããå ´å Presentational Component 㨠Container Component ãè³ã«ãããã¨ããã¨æãã¾ãã åºæ¬çã«ã³ã³ãã¼ãã³ãã¯ãã®åé¡ã§èãã¾ãããã
Presentational Component ã¨ã¯ãView ã«ç¹åããã³ã³ãã¼ãã³ãã§ãèªèº«ã§ã¯ç¶æ ã¯æã¡ã¾ããã Presentational Component ã§ã¯ãã³ã³ãã¼ãã³ãã«éããç¶æ ãé¤ãç¶æ ãæãããprops ã§æ¸¡ã£ã¦ãããã¼ã¿ã表示ããããã«ãã¾ãã
Container Component ã¨ã¯ãPresentational Component ã«ãã¼ã¿ã渡ããããæ©è½ããããããã¨åã³ã³ãã¼ãã³ãã®æ¯ãèããå®ç¾©ãã¦ããã¾ãã
ãã®ï¼ã¤ã§å¤§æ åé¡ãã¦é²ããã¨ç¶æ 管çã¨ãã§ãæ©ãã ãåºæ¬çã«ããªãã§ãããã ãã ä¾å¤ãåå¨ããã¨ã¯æãã®ã§ãã®é½åº¦ãã¼ã ã§è©±ãããã«ã¼ã«ã決ããã¨è¯ããã¨ãããã¾ãã
Nuxt ã§éçºããå ´åãåã¯åºæ¬çã«pages ã« Container ã®å½¹å²ããããã¦ãã¾ãã ã³ã³ãã¼ãã³ãã«éãã¦ããªãç¶æ ããã¸ãã¯ã Vuex ã«ããæä½ã¯å ¨ã¦ pages ã§è¡ãããã«ãã¦ããããããããã¨ã§ pages ããã³ã³ãã¼ãã³ãã¸ä¸ããä¸ã«æµãããããªã¤ã¡ã¼ã¸ã§éçºãé²ãããã¦è¯ãã§ãã ãã ãpages ãè¥å¤§åããå ´åãããã®ã§ãã®ãããªå ´å㯠Container Component ãä½æããpages 㨠Presentational Component ã®éã«æãã§ãã¾ãã
ä¾åãå°ãªããã
ããã¯ãåå©ç¨æ§ã«ãé¢ãã£ã¦ãã話ã§ãã ä¾åã大ããã¨åå©ç¨æ§ãããããããä¸å®ã®å ´æã§ãã使ããªããªã£ã¦ãã¾ããã¡ãªãããããã¾ãã ããã¦ä¾åã大ããã³ã¼ãã¯ã¬ãã¥ã¢ã¼ã®ã³ã¹ããä¸ããã¾ãã ä¾åãã¦ããç®æã®ã³ã¼ããèªã¿ãã£ããã¨ç解ãã¦ããªãã¨æ£ããã¬ãã¥ã¼ãã§ããªããã¡ãªãããããã¾ãã
ãªã®ã§ãåå©ç¨æ§ã¯èããªãã¦ãããã§ããããªãã¹ãä¾åã¯å°ãªããã¾ãããã ãããããã¨ã§ãä¿®æ£ãªã©ãç¨æã«è¡ãã¾ãã
ãªãã¹ãç¶æ ãæããªãããã«ãã
ç¶æ ãæã¤ã¨ãããã¨ã¯ããã ãã§ãã®ç¶æ ã«æ¯ãåãããçºä¾åã大ããã¨ãããã¨ã§ãã ç¶æ ããããããã©ãã責åã§èããç¶æ ããã£ã¦è¯ããè¯ããªããããã£ããã¨èãã¾ãããã ããã¾ã§ãè¸ã¾ãã¦èããã°ç¶æ ãæã¤ã¹ããã©ããã¯ããããã¨æãã¾ãã
ã³ã³ãã¼ãã³ããä½æããæã¯ããã£ããã¨ãã®ã³ã³ãã¼ãã³ãã®è²¬åãªã©ãèãã¦ç²åº¦ããã¼ã¿ã®æã¡æ¹ãèãã¦ä½æãã¾ãããã
ãããã«
Vuex ã®ã«ã¼ã«ã Componentã®è¨è¨ã«ã¤ãã¦æ¸ãã¾ããããããã ã£ãã§ããããï¼ ä»ã«ãèãã¦ãããã¨ã¯ããã¨æãã®ã§ãããè¨èªåã§ãã¦ããªãã®ã§ä»åã¯ãããããã«ããããã¨æãã¾ãã ç¶æ 管çãã³ã³ãã¼ãã³ãã®è¨è¨ã¯ ãã©ã¤ï¼ã¨ã©ã¼ãç¹°ãè¿ãã¦ãããã身ã«ã¤ããæ¹æ³ã¯ãªãã¨åã¯èãã¦ãã¾ãã ãªã®ã§ããã£ã±ã失æãã¦èªåãªãã®ã«ã¼ã«ãè¦ã¤ããããããã«ãªãã¾ãããã ãã®è¨äºãã©ããã§å½¹ã«ç«ã¤ã¨å¹¸ãã§ãã
ç¾å¨ãback check ãã¼ã ã§ã¯ãã¨ã³ã¸ãã¢ã®åéããã¦ããã¾ãã åã¨ä¸ç·ã«ããã³ãã¨ã³ããæã¾ã§èªãå°½ããããããã³ãã¨ã³ãã¨ã³ã¸ãã¢ãã¬ãã«ã®é«ãããã³ãã¨ã³ããã¼ã ã¨æé«ã®ãããã¯ããä½ã£ã¦è¡ããããµã¼ãã¼ãµã¤ãã¨ã³ã¸ãã¢ã¯æ¯éãå¿åãé¡ãããããã¾ãï¼