ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- Vue ã¤ã³ã¹ã¿ã³ã¹
- ãã³ãã¬ã¼ãæ§æ
- ç®åºããããã£ã¨ã¦ã©ããã£
- ã¯ã©ã¹ã¨ã¹ã¿ã¤ã«ã®ãã¤ã³ãã£ã³ã°
- æ¡ä»¶ä»ãã¬ã³ããªã³ã°
- ãªã¹ãã¬ã³ããªã³ã°
- ã¤ãã³ããã³ããªã³ã°
- ãã©ã¼ã å ¥åãã¤ã³ãã£ã³ã°
- ã³ã³ãã¼ãã³ãã®åºæ¬
ã³ã³ãã¼ãã³ãã®è©³ç´°
- ã³ã³ãã¼ãã³ãã®ç»é²
- ããããã£
- ã«ã¹ã¿ã ã¤ãã³ã
- ã¹ããã
- åç & éåæã³ã³ãã¼ãã³ã
- ç¹å¥ãªåé¡ã«å¯¾å¦ãã
ãã©ã³ã¸ã·ã§ã³ã¨ã¢ãã¡ã¼ã·ã§ã³
- Enter/Leave ã¨ãã©ã³ã¸ã·ã§ã³ä¸è¦§
- ç¶æ ã®ãã©ã³ã¸ã·ã§ã³
åå©ç¨ã¨æ§æ
- ããã¯ã¹ã¤ã³
- ã«ã¹ã¿ã ãã£ã¬ã¯ãã£ã
- æç»é¢æ°ã¨JSX
- ãã©ã°ã¤ã³
- ãã£ã«ã¿ã¼
ãã¼ã«
- åä¸ãã¡ã¤ã«ã³ã³ãã¼ãã³ã
- ãã¹ã
- TypeScript ã®ãµãã¼ã
- ãããã¯ã·ã§ã³ç°å¢ã¸ã®é ä¿¡
ã¹ã±ã¼ã«ã¢ãã
- ã«ã¼ãã£ã³ã°
- ç¶æ 管ç
- ãµã¼ããµã¤ãã¬ã³ããªã³ã°
- ã»ãã¥ãªãã£
å é¨
- ãªã¢ã¯ãã£ãã®æ¢æ±
移è¡
- Vue 1.x ããã®ç§»è¡
- Vue Router 0.7.x ããã®ç§»è¡
- Vuex 0.6.x ãã 1.0 ã¸ã®ç§»è¡
ãã®ä»
- ä»ã®ãã¬ã¼ã ã¯ã¼ã¯ã¨ã®æ¯è¼
- Vue.js ã³ãã¥ããã£ã¸åå ãã¾ããã!
- ãã¼ã ã«ä¼ãã
v2.x 以åã®ããã¥ã¡ã³ãã§ãã v3.x ã®ããã¥ã¡ã³ããè¦ããå ´åã¯ãã¡ã
ç¹å¥ãªåé¡ã«å¯¾å¦ãã
æçµæ´æ°æ¥: 2020å¹´5æ16æ¥
ãã®ãã¼ã¸ã¯ãã§ã«ã³ã³ãã¼ãã³ãã®åºæ¬ãèªãã§ãããã¨ãåæã«æ¸ãã¦ãã¾ããããã¾ã èªãã§ããªãã®ãªããå ã«èªã¿ã¾ãããã
ç¹å¥ãªåé¡ãã¤ã¾ãçããç¶æ³ã«å¯¾å¦ããããã®ãã®ãã¼ã¸ã®å ¨ã¦ã®æ©è½ã¯ãæã« Vue ã®ã«ã¼ã«ãå¤å°ãªãã¨ãæ²ãããã¨ã«ãªãã¾ããããã注æãã¦æ¬²ããã®ãããããã¯å ¨ã¦ãã¡ãªãããå±éºãªç¶æ³ãããããå¾ãã¨ãããã¨ã§ãããããã®ãã¤ãã¹çãªé¢ã¯ããããã®ã±ã¼ã¹ã§æ³¨æããã¦ããã®ã§ããã®ãã¼ã¸ã§ç´¹ä»ãããããããã®æ©è½ã使ç¨ããã¨æ±ºããã¨ãã¯å¿ã«æ¢ãã¦ããã¦ãã ããã
è¦ç´ & ã³ã³ãã¼ãã³ãã¸ã®ã¢ã¯ã»ã¹
ã»ã¨ãã©ã®ã±ã¼ã¹ã§ãä»ã®ã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ã¸ã®ã¢ã¯ã»ã¹ã DOM è¦ç´ ãæåæä½ãããã¨ãé¿ããã®ããã¹ãã§ãããããããããé©åãªå ´åãããã¾ãã
ã«ã¼ãã¤ã³ã¹ã¿ã³ã¹ã¸ã®ã¢ã¯ã»ã¹
new Vue
ã¤ã³ã¹ã¿ã³ã¹ã®å
¨ã¦ã®ãµãã³ã³ãã¼ãã³ãããã$root
ããããã£ãç¨ãã¦ã«ã¼ãã¤ã³ã¹ã¿ã³ã¹ã¸ã¢ã¯ã»ã¹ã§ãã¾ããä¾ãã°ããã®ã«ã¼ãã¤ã³ã¹ã¿ã³ã¹ãè¦ã¦ãã ãã:
// ã«ã¼ã Vue ã¤ã³ã¹ã¿ã³ã¹
new Vue({
data: {
foo: 1
},
computed: {
bar: function () { /* ... */ }
},
methods: {
baz: function () { /* ... */ }
}
})
å ¨ã¦ã®ãµãã³ã³ãã¼ãã³ãã¯ãã®ã¤ã³ã¹ã¿ã³ã¹ã«ã¢ã¯ã»ã¹ãããã¨ãã§ããã°ãã¼ãã«ã¹ãã¢ã¨ãã¦ä½¿ããã¨ãã§ãã¾ã:
// ã«ã¼ããã¼ã¿ã®åå¾
this.$root.foo
// ã«ã¼ããã¼ã¿ã®è¨å®
this.$root.foo = 2
// ã«ã¼ãç®åºããããã£ã¸ã®ã¢ã¯ã»ã¹
this.$root.bar
// ã«ã¼ãã¡ã½ããã®å¼ã³åºã
this.$root.baz()
ããã¯ãã¢ãä¸æ¡ãã®ã³ã³ãã¼ãã³ãã§æ§æãããé常ã«å°ããã¢ããªã±ã¼ã·ã§ã³ã¨ãã¦ã¯ä¾¿å©ããããã¾ããããä¸ã大è¦æ¨¡ã®ã¢ããªã±ã¼ã·ã§ã³ã«ã¹ã±ã¼ã«ããã¥ããã§ãããªã®ã§ç§éã¯ã»ã¨ãã©ã®ã±ã¼ã¹ã§ã¹ãã¼ãã管çããããã«Vuexã®ä½¿ç¨ãå¼·ããããããã¦ãã¾ãã
親ã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ã¸ã®ã¢ã¯ã»ã¹
$root
ã¨ä¼¼ãããã«ã$parent
ããããã£ã¯åãã親ã¤ã³ã¹ã¿ã³ã¹ã¸ã¢ã¯ã»ã¹ããããã«ä½¿ããã¾ããããã¯ããããã£ã§ãã¼ã¿ã渡ããã¨ã¸ã®æ æ°ãªä»£æ¿æ段ã¨ãã¦é
åãããã¨ã§ãããã
ã»ã¨ãã©ã®ã±ã¼ã¹ã§ã親ã¸ã®ã¢ã¯ã»ã¹ã¯ã¢ããªã±ã¼ã·ã§ã³ã®ãããã°ãç解ãããé£ãããã¾ããç¹ã«ãããªãã親ã®ãã¼ã¿ãå¤åãããå ´åã¯ãªãããã§ããå¾ã ã«ãªã£ã¦ãã®ã³ã³ãã¼ãã³ããæ±ãã¨ãããã®å¤åãã©ãããçãããã®ãªã®ããç解ãããã¨ã¯ã¨ã¦ãé£ãããã¨ã§ãããã
ãããã¨ãããå ±æã³ã³ãã¼ãã³ãã©ã¤ãã©ãªã®å ´åã¯ããããé©åã§ãããããããªãå ´åãããã¾ããä¾ãã°ãä»®æ³ç㪠Google Map ã³ã³ãã¼ãã³ãã®ããã«ãHTML ãæç»ãã代ããã« JavaScript ã® API ãæ±ãæ½è±¡ã³ã³ãã¼ãã³ãã§:
<google-map>
<google-map-markers v-bind:places="iceCreamShops"></google-map-markers>
</google-map>
<google-map>
ã³ã³ãã¼ãã³ãã¯å
¨ã¦ã®ãµãã³ã³ãã¼ãã³ããã¢ã¯ã»ã¹ããå¿
è¦ããã map
ããããã£ãå®ç¾©ãã¦ãã¾ãããã®å ´åã<google-map-markers>
ã¯å°å³ä¸ã«ãã¼ã«ã¼ãè¨å®ãããã this.$parent.getMap
ã®ãããªæ¹æ³ã§ map ããããã£ã«ã¢ã¯ã»ã¹ããããã¨ã§ãããããããããã®ãã¿ã¼ã³ãã¿ããã¨ãã§ãã¾ãã
ãããããã®ãã¿ã¼ã³ã§ä½æãããã³ã³ãã¼ãã³ãã¯ãã¯ãæ¬è³ªçã«å£ãããããªãã¨ãããã¨ãè¦ãã¦ããã¦ãã ãããä¾ãã°ã<google-map-region>
ã¨ããæ°ããã³ã³ãã¼ãã³ãã追å ãããã¨ãã¤ã¡ã¼ã¸ãã¦ãã ãããããã¦ã<google-map-markers>
ã <google-map-region>
å
ã«ç¾ããã¨ãããã®é åå
ã®ãã¼ã«ã¼ã®ã¿æç»ãã¹ãã§ã:
<google-map>
<google-map-region v-bind:shape="cityBoundaries">
<google-map-markers v-bind:places="iceCreamShops"></google-map-markers>
</google-map-region>
</google-map>
ãã®ã¨ã <google-map-markers>
ã®å
é¨ã§ãããªãã¯ãã®ãããªããã¯ã«è¡ãçãããããã¾ãã:
var map = this.$parent.map || this.$parent.$parent.map
ãã®ããã¯ã¯ããã«æã«è² ããªããªãã¾ããã³ã³ããã¹ãã®æ å ±ãåå«ã®ã³ã³ãã¼ãã³ãã«æ·±ãæä¾ããããã§ããç§éã¯ä»£ããã«ä¾åæ§ã®æ³¨å ¥ãå§ãã¾ãã
åã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ã¨åè¦ç´ ã¸ã®ã¢ã¯ã»ã¹
ããããã£ã¨ã¤ãã³ããåå¨ããã«ãé¢ããããã¨ãã©ã JavaScript ã§ç´æ¥åã³ã³ãã¼ãã³ãã«ã¢ã¯ã»ã¹ããå¿
è¦ãããããããã¾ããããã®ããã« ref
å±æ§ã使ããåã³ã³ãã¼ãã³ãã«ãªãã¡ã¬ã³ã¹ ID ãå²ãå½ã¦ããã¨ãã§ãã¾ããä¾ãã°:
<base-input ref="usernameInput"></base-input>
ä»ãã® ref
ãå®ç¾©ããã³ã³ãã¼ãã³ãã§ããã®ããã«:
this.$refs.usernameInput
<base-input>
ã¤ã³ã¹ã¿ã³ã¹ã«ã¢ã¯ã»ã¹ãããã¨ãã§ããããã«ãªãã¾ããä¾ãã°ããªããããã°ã©ã ã«ãã£ã¦ã親ã³ã³ãã¼ãã³ããããã®ã¤ã³ããããã©ã¼ã ã«ãã©ã¼ã«ã¹ãããã¨ãã«å½¹ç«ã¡ã¾ãããã®å ´åã<base-input>
ã³ã³ãã¼ãã³ãã¯å
é¨ã®ç¹å®è¦ç´ ã¸ã®ã¢ã¯ã»ã¹ãæä¾ããããã親ã¨åæ§ã«æ¬¡ã®ããã« ref
ã使ãããããã¾ãã:
<input ref="input">
ããã¦è¦ªã«ãã£ã¦ä½¿ç¨ãããã¡ã½ãããå®ç¾©ãã¦:
methods: {
// 親ããã¤ã³ãããè¦ç´ ããã©ã¼ã«ã¹ããããã«ä½¿ããã
focus: function () {
this.$refs.input.focus()
}
}
ãã®ãããªã³ã¼ãã§ã親ã³ã³ãã¼ãã³ãã« <base-input>
å
é¨ã® input è¦ç´ ã«ãã©ã¼ã«ã¹ããã¾ã:
this.$refs.usernameInput.focus()
ref
ã v-for
ã¨å
±ã«ä½¿ç¨ãããã¨ããããªããå¾ãåç
§ã¯ãã¼ã¿ã½ã¼ã¹ããã©ã¼ãªã³ã°ããåã³ã³ãã¼ãã³ãã®é
åã§ãããã
$refs
ã¯ã³ã³ãã¼ãã³ãã®æç»å¾ã«ãã¼ã¿ãåæ ãããã ãã§ããªã¢ã¯ãã£ãã§ã¯ããã¾ãããåã³ã³ãã¼ãã³ãã¸ã®ç´æ¥æä½ã®ããã®ãéé¿ç¨ãããã®ãããªæå³åãã§ã(ãã³ãã¬ã¼ãå
ã¾ãã¯ç®åºããããã£ãã$refs
ã«ã¢ã¯ã»ã¹ããã®ã¯é¿ããã¹ãã§ã)ã
ä¾åæ§ã®æ³¨å ¥
å ã»ã©ã親ã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ã¸ã®ã¢ã¯ã»ã¹ã説æããã¨ãã以ä¸ã®ãããªä¾ãåºãã¾ãã:
<google-map>
<google-map-region v-bind:shape="cityBoundaries">
<google-map-markers v-bind:places="iceCreamShops"></google-map-markers>
</google-map-region>
</google-map>
ãã®ã³ã³ãã¼ãã³ãã§ã<google-map>
ã®å
¨ã¦ã®åå«ã¯å°å³ã®ã©ã®é¨åã«ä½ç¨ãããã®ããç¥ãããã« getMap
ã¸ã¢ã¯ã»ã¹ãããã¨ãå¿
è¦ã¨ãã¦ãã¾ãããä¸å¹¸ã«ã $parent
ããããã£ã®ä½¿ç¨ã¯ãããæ·±ããã¹ããããã³ã³ãã¼ãã³ãã«é©åã§ãã¾ããã§ããããã®ç¹ãããã2ã¤ã®æ°ããã¤ã³ã¹ã¿ã³ã¹ãªãã·ã§ã³ãprovide
㨠inject
ã®ä½¿ç¨ã«ãããä¾åæ§ã®æ³¨å
¥ãå½¹ç«ã¤ã¨ããã§ãã
provide
ãªãã·ã§ã³ã¯åå«ã®ã³ã³ãã¼ãã³ãã«æä¾ããããã¼ã¿ãã¡ã½ãããç¹å®ããã¾ãããã®å ´åããã㯠<google-map>
å
ã«ãã getMap
ã§ã:
provide: function () {
return {
getMap: this.getMap
}
}
ãã®ã¨ãå
¨ã¦ã®åå«ã§ãç§éã¯ã¤ã³ã¹ã¿ã³ã¹ã«è¿½å ãããç¹å®ã®ããããã£ãåãåããã inject
ãªãã·ã§ã³ã使ããã¨ãã§ãã¾ã:
inject: ['getMap']
以ä¸ã®å®ä¾ã¯ãããã確èªã§ãã¾ãã$parent
ã使ã以ä¸ã®å©ç¹ã¯ <google-map>
ã¤ã³ã¹ã¿ã³ã¹å
¨ä½ãæããã¨ãªããã©ã®åå«ã³ã³ãã¼ãã³ãããã§ã getMap
ã«ã¢ã¯ã»ã¹ã§ãããã¨ã§ããããã¯åã³ã³ãã¼ãã³ããä¾åããä½ããå¤æ´ãåé¤ãããããããªãã¨ããææãç¡ãããããå®å
¨ã«ã³ã³ãã¼ãã³ããéçºã§ããããã«ãã¾ãããããã®ã³ã³ãã¼ãã³ãéã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ãã¡ããã©ããããã£
ãç¨ããããã«æ確ã«å®ç¾©ããã¾ãã
å®éã以ä¸ãé¤ãã°ãããã°âåºç¯å²ã®ããããã£âã®ãããªãã®ã¨ããä¾åæ§ã®æ³¨å ¥ã¨èãããã¨ãã§ãã¾ã:
- ç¥å ã®ã³ã³ãã¼ãã³ãã¯ã©ã®åå«ãèªåãæä¾ããããããã£ã使ã£ã¦ããã®ããç¥ãå¿ è¦ãããã¾ããã
- åå«ã®ã³ã³ãã¼ãã³ãã¯æ³¨å ¥ãããããããã£ãã©ããããã¦ããã®ããç¥ãå¿ è¦ãããã¾ããã
ããããä¾åæ§ã®æ³¨å
¥ã«ã¯ä¸é½åãªç¹ãããã¾ãã ä¾åæ§ã®æ³¨å
¥ã¯ã¢ããªã±ã¼ã·ã§ã³ã®ã³ã³ãã¼ãã³ããç¾å¨ã®ç¶æ
ã«å¯çµåããããªãã¡ã¯ã¿ãªã³ã°ãé£ããããã¾ããããã¦æä¾ãããããããã£ã¯ãªã¢ã¯ãã£ãã§ã¯ããã¾ãããããã¯è¨è¨ä¸ã®çç±ã«ãããã®ã§ããä¸å¤®ãã¼ã¿ã¹ãã¢ãä½ãããã«ä¾åæ§ã®æ³¨å
¥ã使ããã¨ã¯ãåãç®çã®ããã«$root
ã使ããã¨ã¨åããããã¢ããªã±ã¼ã·ã§ã³ã®ã¹ã±ã¼ã«ãé£ãããã¾ããããã¢ããªã±ã¼ã·ã§ã³ã«ç¹å®ã®ããããã£ãã·ã§ã¢ãããã®ãªãããããã¯ããå
ç¥ã«æä¾ãããã¼ã¿ãæ´æ°ãããã®ãªãããã®ã¨ãã¯çµã¿è¾¼ã¿ã®æ©è½ããããããVuexã®ãããªæ¬ç©ã®ç¶æ
管çã½ãªã¥ã¼ã·ã§ã³ãå¿
è¦ã¨ããããå
åã§ãã
ä¾åæ§ã®æ³¨å ¥ã«ã¤ãã¦ããå¦ã³ããã®ãªãããã® API ããã¥ã¡ã³ããåç §ãã¦ãã ããã
ããã°ã©ã çãªã¤ãã³ããªã¹ãã¼
ä»ã®ã¨ãããv-on
ã«ããçºç«ããã $emit
ã®ä½¿ç¨æ³ãè¦ã¦ãã¾ãããããã Vue ã¤ã³ã¹ã¿ã³ã¹ã¯ä»¥ä¸ã®ãããªãä»ã®ã¤ãã³ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ã¡ã½ãããæä¾ãã¦ãã¾ãã
- ç¹å®ã®ã¤ãã³ããç£è¦ãã
$on(eventName, eventHandler)
- ä¸åº¦ã®ã¤ãã³ãããç£è¦ããªã
$once(eventName, eventHandler)
- ã¤ãã³ãã®ç£è¦ãããã
$off(eventName, eventHandler)
é常ãããã使ç¨ããå¿ è¦ã¯ããã¾ããããæåã§ã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ãç£è¦ããå¿ è¦ãããã¨ãã«ç¨ãããã¨ãã§ãã¾ãããããã¯ã³ã¼ãã®çµ±åãã¼ã«ã¨ãã¦ãå½¹ç«ã¡ã¾ããä¾ãã°ãæã ãµã¼ããã¼ãã£ã©ã¤ãã©ãªã使ç¨ããããã«ãã®ãããªãã¿ã¼ã³ã«ééããããããã¾ãã:
// ä¸æ¦DOMã«ãã¦ã³ããããã¨ãã
// datepicker ãã¤ã³ãããè¦ç´ ã«ç´ä»ãã
mounted: function () {
// Pikaday ã¯ãµã¼ããã¼ãã£ã®æ¥ä»é¸æã®ã©ã¤ãã©ãªã§ã
this.picker = new Pikaday({
field: this.$refs.input,
format: 'YYYY-MM-DD'
})
},
// ã³ã³ãã¼ãã³ããç ´æ£ãããç´åã«ã
// datepicker ãç ´æ£ããã¾ã
beforeDestroy: function () {
this.picker.destroy()
}
ããã«ã¯2ã¤ã®æ½å¨çãªåé¡ãããã¾ã:
- ã©ã¤ããµã¤ã¯ã«ããã¯ã
picker
ãªãã¸ã§ã¯ãã«ã¢ã¯ã»ã¹ããå¿ è¦ãããå¯è½æ§ãããã¨ããã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ã«ãããä¿åããå¿ è¦ãããã¾ããé ·ãã¯ãªãã§ãããç ©éã«æããããããããã¾ããã - ã»ããã¢ããã³ã¼ãããã¯ãªã¼ã³ã¢ããã³ã¼ãã¨ã¯å¥ã«ä¿ããã¦ãããããã¯ãã»ããã¢ãããããã®ãããã°ã©ã ã§ã¯ãªã¼ã³ã¢ãããããã¨ãããå°é£ã«ãã¦ãã¾ãã
ããã°ã©ã çãªãªã¹ãã¼ã使ç¨ãããã¨ã§ä¸¡æ¹ã®åé¡ã解決ãããã¨ãã§ãã¾ã:
mounted: function () {
var picker = new Pikaday({
field: this.$refs.input,
format: 'YYYY-MM-DD'
})
this.$once('hook:beforeDestroy', function () {
picker.destroy()
})
}
ãã®æ¦ç¥ã使ç¨ãããã¨ã«ãã£ã¦ãããã¤ãã®ã¤ã³ãããè¦ç´ 㧠Pikaday ã使ç¨ãããã¨ãã§ãããã®ã©ã¤ããµã¤ã¯ã«èªèº«ã®ã³ã¼ãã§ãåæ°ããã¤ã³ã¹ã¿ã³ã¹ã¯èªåçã«ã¯ãªã¼ã³ã¢ããã§ãã¾ãã
mounted: function () {
this.attachDatepicker('startDateInput')
this.attachDatepicker('endDateInput')
},
methods: {
attachDatepicker: function (refName) {
var picker = new Pikaday({
field: this.$refs[refName],
format: 'YYYY-MM-DD'
})
this.$once('hook:beforeDestroy', function () {
picker.destroy()
})
}
}
å
¨ã¦ã®ã³ã¼ããè¼ã£ã¦ãããã®ä¾ãè¦ã¦ãã ãããããã注æãã¦æ¬²ããã®ãããã1ã¤ã®ã³ã³ãã¼ãã³ãå
ã§å¤ãã®ã»ããã¢ãããã¯ãªã¼ã³ã¢ãããããªããã°ãªããªãå ´åããã¹ããªè§£æ±ºçã¯ããã¦ãããç´°ååããã³ã³ãã¼ãã³ããä½ããã¨ã§ãããã®ã±ã¼ã¹ã§ã¯ãåå©ç¨å¯è½ãª <input-datepicker>
ã³ã³ãã¼ãã³ããä½ããã¨ããããããã¾ãã
ããããã°ã©ã çãªãªã¹ãã¼ã®è©³ç´°ãå¦ã¶ãªããã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¤ãã³ãã® API ããã§ãã¯ãã¦ãã ããã
Vue ã®ã¤ãã³ãã·ã¹ãã ã¯ãã©ã¦ã¶ã®ã¤ãã³ãã¿ã¼ã²ãã API ã¨ã¯ç°ãªã£ã¦ãããã¨ã«æ³¨æãã¦ãã ããããããã¯$emit
, $on
, $off
ã¨ä¼¼ãããã«åä½ãã¾ãããdispatchEvent
, addEventListener
, removeEventListener
ã®ã¨ã¤ãªã¢ã¹ã§ã¯ããã¾ããã
循ç°åç §
å帰çã³ã³ãã¼ãã³ã
ã³ã³ãã¼ãã³ãã¯èªèº«ããã³ãã¬ã¼ãã§å帰çã«å¼ã³åºããã¨ãã§ãã¾ããname
ãªãã·ã§ã³ã使ç¨ãããã¨ã«ãã£ã¦ãããã¯å¯è½ã§ã:
name: 'unique-name-of-my-component'
Vue.component
ãç¨ãã¦ã°ãã¼ãã«ã«ã³ã³ãã¼ãã³ããç»é²ããã¨ããã°ãã¼ãã« ID ã¯èªåçã«ãã³ã³ãã¼ãã³ãã® name
ãªãã·ã§ã³ã¨ãã¦ã»ããããã¾ãã
Vue.component('unique-name-of-my-component', {
// ...
})
注æããªãã¨ãå帰çãªã³ã³ãã¼ãã³ããç¡éã«ã¼ãã«ç¹ããå¯è½æ§ãããã¾ã:
name: 'stack-overflow',
template: '<div><stack-overflow></stack-overflow></div>'
ä¸è¨ã®ãããªã³ã³ãã¼ãã³ãã¯âmax stack size exceededâã¨ã©ã¼ã«çµããã§ãããããªã®ã§å¿
ãå帰çãªå¼ã³åºãã¯æ¡ä»¶ä»ãã«ãã¾ããã(ä¾ãã°æçµçã« false
ã«ãªã v-if
ã使ç¨ããããã«)ã
ã³ã³ãã¼ãã³ãéã®å¾ªç°åç §
ããªã㯠Finder ããã¡ã¤ã«ã¨ã¯ã¹ããã¼ã©ã®ãããªãã¡ã¤ã«ãã£ã¬ã¯ããªããªã¼ãæ§ç¯ãã¦ããã¨ãã¾ãããããã®ãã³ãã¬ã¼ãã®ãã㪠tree-folder
ã³ã³ãã¼ãã³ããæã¤ããããã¾ãã:
<p>
<span>{{ folder.name }}</span>
<tree-folder-contents :children="folder.children"/>
</p>
tree-folder-contents
ã¯ä»¥ä¸ã®ãããªãã³ãã¬ã¼ãã§ã:
<ul>
<li v-for="child in children">
<tree-folder v-if="child.children" :folder="child"/>
<span v-else>{{ child.name }}</span>
</li>
</ul>
ããè¦ãã¨ããããã®ã³ã³ãã¼ãã³ããå®éã«ããããä»ã®ã¬ã³ããªã³ã°ããªã¼ã®åå«ã¨ç¥å
ã«ãªã£ã¦ãã¾ãããçç¾ãã¦ã¾ããï¼Vue.component
ã§ã°ãã¼ãã«ã«ã³ã³ãã¼ãã³ããç»é²ããã¨ãããã®çç¾ã¯èªåçã«è§£æ±ºããã¾ããä½ãè¨ã£ã¦ããã®ãåãããªããªãããã以éèªãã®ããããã¨ãã§ãã¾ãã
ãããããããªãããä¾ãã° Webpack ã Browserify çµç±ã§ãã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã使ç¨ããã³ã³ãã¼ãã³ãã require/import ãããªãã°ã以ä¸ã®ãããªã¨ã©ã¼ã«ééããã§ããã:
Failed to mount component: template or render function not defined.
ä½ãèµ·ãã£ããã説æããããã«ãã³ã³ãã¼ãã³ã A 㨠B ãå¼ã³åºãã¦ã¿ã¾ããããã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã¯ã³ã³ãã¼ãã³ã A ãå¿ è¦ã¨ããã¨èªèãã¾ãããããã³ã³ãã¼ãã³ã A ã¯ã³ã³ãã¼ãã³ã B ãå¿ è¦ã¨ãã¾ãããããã³ã³ãã¼ãã³ã B ã¯ã³ã³ãã¼ãã³ã A ãå¿ è¦ã¨ãã¾ãããããã³ã³ãã¼ãã³ã A ã¯ã³ã³ãã¼ãã³ã B ãå¿ è¦ã¨ãã¾ã(以ä¸ç¥)ãæåã«ä»ã®ãã®ã解決ãããã¨ãªããããããã®ã³ã³ãã¼ãã³ããå®å ¨ã«è§£æ±ºããæ¹æ³ãããããã«ã«ã¼ãã§è©°ã¾ã£ã¦ãã¾ã£ã¦ãã¾ãããããç´ããããã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã«ãã³ã³ãã¼ãã³ã A ã¯æçµçã«ã³ã³ãã¼ãã³ã B ãå¿ è¦ã¨ãã¾ãããB ãæåã«è§£æ±ºããå¿ è¦ã¯ããã¾ãããã¨ãããã¨ãæããå¿ è¦ãããã¾ãã
ä»åã¯ããã®ãã¨ã tree-folder
ã³ã³ãã¼ãã³ãã«æãã¦ã¿ã¾ããããç§éã¯ãã®çç¾ãçã¿åºãåããtree-folder-contents
ã³ã³ãã¼ãã³ãã ã¨ãããã¨ãç¥ã£ã¦ãã¾ãããªã®ã§ãbeforeCreate
ã©ã¤ããµã¤ã¯ã«ããã¯ã tree-folder-contents
ã³ã³ãã¼ãã³ããç»é²ããã¾ã§å¾
ã£ã¦ã¿ã¾ããã:
beforeCreate: function () {
this.$options.components.TreeFolderContents = require('./tree-folder-contents.vue').default
}
ãããã¯å¥ã®æ¹æ³ã¨ãã¦ãã³ã³ãã¼ãã³ãããã¼ã«ã«ã«ç»é²ããã¨ãã« Webpack ã®éåæ import
ã使ç¨ãããã¨ãã§ãã¾ã:
components: {
TreeFolderContents: () => import('./tree-folder-contents.vue')
}
ããã§åé¡ã解決ããã¾ããï¼
代æ¿ãã³ãã¬ã¼ãå®ç¾©
ã¤ã³ã©ã¤ã³ãã³ãã¬ã¼ã
inline-template
å±æ§ãåã³ã³ãã¼ãã³ãã®ä¸ã«åå¨ããã¨ããåã³ã³ãã¼ãã³ããå¥ã®ã³ã³ãã³ããã¦æ±ãããããããèªèº«ã®ãã³ãã¬ã¼ãã¨ãã¦ããã使ç¨ãã¾ããããã¯ãããã³ãã¬ã¼ãä½æããã¬ãã·ãã«ã«ãã¾ãã
<my-component inline-template>
<div>
<p>These are compiled as the component's own template.</p>
<p>Not parent's transclusion content.</p>
</div>
</my-component>
ã¤ã³ã©ã¤ã³ãã³ãã¬ã¼ãã¯ãVue ãã¢ã¿ããããã DOM è¦ç´ ã®å å´ã§å®ç¾©ããå¿ è¦ãããã¾ãã
ããããinline-template
ã¯ãã³ãã¬ã¼ãã®ã¹ã³ã¼ãã®æ¨è«ãé£ãããã¾ãããã¹ããã©ã¯ãã£ã¹ã¨ãã¦ãtemplate
ãªãã·ã§ã³ã使ç¨ãã¦ã³ã³ãã¼ãã³ãå
é¨ã§ãã³ãã¬ã¼ãå®ç¾©ããããã«ãã¦ãã ãããã¾ãã¯.vue
ãã¡ã¤ã«ã®<template>
è¦ç´ ã§å®ç¾©ããã®ãããã§ãããã
X- ãã³ãã¬ã¼ã
ãã³ãã¬ã¼ããå®ç¾©ããå¥ã®æ¹æ³ã¯ãtype å±æ§text/x-template
ãç¨ããã¹ã¯ãªããè¦ç´ ã®å
é¨ã§å®ç¾©ãããã¨ã§ãããã®ã¨ããid ã«ãã£ã¦ãã³ãã¬ã¼ããåç
§ããå¿
è¦ãããã¾ããä¾ãã°:
<script type="text/x-template" id="hello-world-template">
<p>Hello hello hello</p>
</script>
Vue.component('hello-world', {
template: '#hello-world-template'
})
x-template ã¯ãVue ãã¢ã¿ããããã DOM è¦ç´ ã®å¤å´ã§å®ç¾©ããå¿ è¦ãããã¾ãã
ãããã¯å¤§è¦æ¨¡ãªãã³ãã¬ã¼ããå¿ è¦ã¨ãããã¢ãé常ã«å°ããªã¢ããªã±ã¼ã·ã§ã³ã§å½¹ç«ã¡ã¾ãããããä¸æ¹ã§é¿ããããã¹ãã§ãããã¾ãããªããªãããããã¯ã³ã³ãã¼ãã³ãå®ç¾©ãããã³ãã¬ã¼ããåé¢ãããããã§ãã
æ´æ°ãã³ã³ããã¼ã«ãã
Vue ã®ãªã¢ã¯ãã£ãã·ã¹ãã ã®ãããã§ããã¤ããã¤æ´æ°ããããç¥ããã¨ãã§ãã¾ã(ããããªããæ£ç¢ºã«ä½¿ã£ã¦ãããªã)ããããããªã¢ã¯ãã£ããã¼ã¿ãå¤æ´ããã¦ããªãã«ãé¢ãããæ´æ°ãå¼·å¶ãããã¨ããªã©ãç¹å¥ãªã±ã¼ã¹ãããã¾ãã
å¼·å¶æ´æ°
ãã Vue ã§å¼·å¶æ´æ°ãããå¿ è¦ãªå ´é¢ã«ééããå ´åã99.99% ã®ã±ã¼ã¹ã§ããªãã¯ä½ããééãã¦ãã¾ãã
é
åãã¾ãã¯ãªãã¸ã§ã¯ã ãã¾ãã¯ä¾ã¨ãã¦data
ã®ãããªãªã¢ã¯ãã£ãã·ã¹ãã ã«ãã£ã¦è¿½è·¡ããã¦ããªãç¶æ
ã«ä¾åãã¦ããããã«ãå¤æ´æ¤åºã®è¦åãèæ
®ãã¦ããªãããããã¾ããã
ããããããä¸è¨ã®å¯è½æ§ãæé¤ããæåã§å¼·å¶æ´æ°ãããé常ã«ç¨ãªç¶æ³ã¨èªèãã¦ãããªãã°ã$forceUpdate
ãç¨ãããã¨ã§å¼·å¶æ´æ°ããããã¨ãã§ãã¾ãã
v-once
ã使ç¨ãããã¼ãã¹ã¿ãã£ãã¯ã³ã³ãã¼ãã³ã
ãã¬ã¼ã³ãª HTML è¦ç´ ãã¬ã³ããªã³ã°ãããã¨ã¯ Vue ã«ããã¦ã¨ã¦ãé«éã§ãããããã¨ãã©ãå¤ãã®éçãªã³ã³ãã³ããå«ãã³ã³ãã¼ãã³ããæã¡ããå ´åãããããããã¾ããããããã®ã±ã¼ã¹ã§ã¯ããã®ããã«ã«ã¼ãè¦ç´ ã« v-once
ãã£ã¬ã¯ãã£ããå ãããã¨ã«ãã£ã¦ä¸åº¦ã ãè©ä¾¡ãããããã¦ãã£ãã·ã¥ããããã¨ãä¿è¨¼ãããã¨ãã§ãã¾ã:
Vue.component('terms-of-service', {
template: `
<div v-once>
<h1>Terms of Service</h1>
... a lot of static content ...
</div>
`
})
å度注æãã¾ããããã®ãã¿ã¼ã³ãå¤ç¨ããªãããã«ãã¦ãã ãããå¤ãã®éçãªå
容ãæç»ããªããã°ãªããªãã¨ãããããã®ã¬ã¢ã±ã¼ã¹ã¯ä¾¿å©ã§ããä¸æ¹ãããªããå®éã«é
ãã¬ã³ããªã³ã°ã«æ°ä»ããªãéãã¯çµ¶å¯¾ã«å¿
è¦ã§ã¯ããã¾ãããããã«ããã¯å¾ã«å¤ãã®æ··ä¹±ã®åå ã«ãªãå¾ãã§ããããä¾ãã°ãv-once
ã«ç²¾éãã¦ããªãéçºè
ããåç´ã«ãã³ãã¬ã¼ãå
ã«ãããè¦éããéçºè
ãæ³åãã¦ã¿ã¦ãã ããããããã¯ãªããã³ãã¬ã¼ããæ£ç¢ºã«æ´æ°ãããªãã®ãã®åå 究æã«æéãè²»ãããã¨ã«ãªãããããã¾ããã