ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- 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 ã®ããã¥ã¡ã³ããè¦ããå ´åã¯ãã¡ã
ã³ã³ãã¼ãã³ãã®ç»é²
æçµæ´æ°æ¥: 2019å¹´7æ22æ¥
ãã®ãã¼ã¸ã¯ ã³ã³ãã¼ãã³ãã®åºæ¬ ãèªã¾ãã¦ãããã¨ãåæã«ãªã£ã¦ãã¾ããã³ã³ãã¼ãã³ããæ±ã£ãäºã®ãªãå ´åã¯ãã¡ãã®ãã¼ã¸ãå ã«èªãã§ãã ããã
ã³ã³ãã¼ãã³ãå
ã³ã³ãã¼ãã³ããç»é²ããã¨ãã¯ããã¤ã§ãååãä¸ãããã¾ããä¾ãã°ãã°ãã¼ãã«ç»é²ã®å ´å:
Vue.component('my-component-name', { /* ... */ })
ã³ã³ãã¼ãã³ãå㯠Vue.component
ã®ç¬¬ä¸å¼æ°ã§ãã
ã³ã³ãã¼ãã³ãã®å½åã¯ãã³ã³ãã¼ãã³ãã®ä½¿ç¨ç®æã«å·¦å³ããã¾ãã(æååãã³ãã¬ã¼ããã¾ãã¯åä¸ãã¡ã¤ã«ã³ã³ãã¼ãã³ãã§ã¯ãªã) DOM ä¸ã§ç´æ¥ã³ã³ãã¼ãã³ãã使ç¨ããå ´åã¯ãW3C rules ã«å¾ã£ãã«ã¹ã¿ã ã¿ã°å(å ¨ã¦å°æåã§ããã¤ãã³ãå«ã¾ãã¦ãããã¨)ãæ¨å¥¨ãã¾ããããã¯ãæ¢ã«åå¨ãããããã¦å°æ¥å®ç¾©ããã HTML è¦ç´ ã¨ã®è¡çªãé²æ¢ããã®ã«å½¹ç«ã¡ã¾ãã
ã¹ã¿ã¤ã«ã¬ã¤ãã§ã¯ãã³ã³ãã¼ãã³ãåã«ã¤ãã¦ã®ãã®ä»ã®æ¨å¥¨é ç®ãè¦ããã¨ãã§ãã¾ãã
å½åã®ã±ã¼ã¹ (Name Casing)
ã³ã³ãã¼ãã³ãåãå®ç¾©ããæã2 ã¤ã®é¸æè¢ãããã¾ã:
ã±ããã±ã¼ã¹ (kebab-case)
Vue.component('my-component-name', { /* ... */ })
ã±ããã±ã¼ã¹ã§ã³ã³ãã¼ãã³ãåãå®ç¾©ããå ´åããã®ã«ã¹ã¿ã è¦ç´ ãåç
§ããæãåæ§ã«ã <my-component-name>
ã®ããã«ãã±ããã±ã¼ã¹ãç¨ããªããã°ããã¾ããã
ãã¹ã«ã«ã±ã¼ã¹ (PascalCase)
Vue.component('MyComponentName', { /* ... */ })
ãã¹ã«ã«ã±ã¼ã¹ã§ã³ã³ãã¼ãã³ããå®ç¾©ããå ´åããã®ã«ã¹ã¿ã è¦ç´ ã®åç
§ã«ã¯ãã©ã¡ãã®ã±ã¼ã¹ãç¨ãããã¨ãã§ãã¾ããããã¯ã <my-component-name>
㨠<MyComponentName>
ã®ã©ã¡ãã許容ããããã¨ãæå³ãã¾ãããã ããDOM å
(ããªãã¡ãæååã§ãªããã³ãã¬ã¼ã) ã«ç´æ¥ä½¿ç¨ããå ´åã«ã¯ãã±ããã±ã¼ã¹ã®ååã®ã¿ãæå¹ãªã®ã§æ³¨æãã¦ãã ããã
ã°ãã¼ãã«ç»é²
ããã¾ã§ã¯ Vue.component
ã ãã使ã£ã¦ã³ã³ãã¼ãã³ããä½æãã¾ãã:
Vue.component('my-component-name', {
// ... options ...
})
ãããã®ã³ã³ãã¼ãã³ã㯠ã°ãã¼ãã«ç»é² ããã¦ãã¾ããããã¯ãç»é²å¾ã«ä½æããããå
¨ã¦ã®ã«ã¼ã Vue ã¤ã³ã¹ã¿ã³ã¹(new Vue
)ã®ãã³ãã¬ã¼ãå
ã§ä½¿ç¨ã§ãããã¨ãæå³ãã¾ããä¾ãã°:
Vue.component('component-a', { /* ... */ })
Vue.component('component-b', { /* ... */ })
Vue.component('component-c', { /* ... */ })
new Vue({ el: '#app' })
<div id="app">
<component-a></component-a>
<component-b></component-b>
<component-c></component-c>
</div>
ããã¯å ¨ã¦ã®ãµãã³ã³ãã¼ãã³ãã«ãé©ç¨ããã¾ããããã 3 ã¤ã®ã³ã³ãã¼ãã³ãå ¨ã¦ã å é¨ã§ç¸äºã« 使ç¨å¯è½ã«ãªããã¨ãæå³ãã¾ãã
ãã¼ã«ã«ç»é²
å¤ãã®å ´åãã°ãã¼ãã«ç»é²ã¯çæ³çã§ã¯ããã¾ãããä¾ãã° Webpack ã®ãããªãã«ãã·ã¹ãã ãå©ç¨ãã¦ããã¨ãã«ãã°ãã¼ãã«ã«ç»é²ããå ¨ã¦ã®ã³ã³ãã¼ãã³ãã¯ããã¨ã使ç¨ããªããªã£ã¦ããä¾ç¶ã¨ãã¦æçµãã«ãã«å«ã¾ãã¦ãã¾ããã¨ã§ããããããã¯ãã¦ã¼ã¶ããã¦ã³ãã¼ãããªãã¦ã¯ãªããªã JavaScript ã®ãã¡ã¤ã«ãµã¤ãºãä¸è¦ã«å¢å ããã¦ãã¾ãã¾ãã
ãã®ãããªå ´åã«ãã³ã³ãã¼ãã³ããç´ ã® JavaScript ãªãã¸ã§ã¯ãã¨ãã¦å®ç¾©ãããã¨ãã§ãã¾ã:
var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
var ComponentC = { /* ... */ }
次ã«ãcomponents
ãªãã·ã§ã³å
ã«ä½¿ãããã³ã³ãã¼ãã³ããå®ç¾©ãã¾ã:
new Vue({
el: '#app',
components: {
'component-a': ComponentA,
'component-b': ComponentB
}
})
components
ãªãã¸ã§ã¯ãã®ããããã®ããããã£ã¯ããã¼ã¯ã«ã¹ã¿ã è¦ç´ ã®ååã«ãªããä¸æ¹ãå¤ã¯ã³ã³ãã¼ãã³ãã®ãªãã·ã§ã³ãªãã¸ã§ã¯ããå«ã¿ã¾ãã
ãã¼ã«ã«ç»é²ãããã³ã³ãã¼ãã³ãã¯ãä»ã®ãµãã³ã³ãã¼ãã³ãå
ã§ã¯ä½¿ç¨ã§ããªã ãã¨ã«æ³¨æãã¦ä¸ãããä¾ãã°ãComponentA
ã ComponentB
å
ã§ä½¿ç¨å¯è½ã«ãããã¨ãã¯ããã®ããã«ä½¿ãå¿
è¦ãããã¾ã:
var ComponentA = { /* ... */ }
var ComponentB = {
components: {
'component-a': ComponentA
},
// ...
}
ãããã¯ãBabel 㨠Webpack ã®ãããªãã®ãç¨ã㦠ES2015 ã¢ã¸ã¥ã¼ã«ãå©ç¨ãã¦ãããªãã°ããã®ããã«ãªãã§ããã:
import ComponentA from './ComponentA.vue'
export default {
components: {
ComponentA
},
// ...
}
ES2015+ ã§ã¯ ComponentA
ã®ãããªå¤æ°åããªãã¸ã§ã¯ãå
é¨ã«é
ç½®ãããã¨ã¯ ComponentA: ComponentA
ã®çç¥è¨æ³ã«ããããå¤æ°ã®ååã¯æ¬¡ã®ã©ã¡ããæå³ãããã¨ã«æ³¨æãã¦ä¸ãã:
- ãã³ãã¬ã¼ãå ã§ä½¿ãããã«ã¹ã¿ã è¦ç´ å
- ã³ã³ãã¼ãã³ããªãã·ã§ã³ãå«ãã å¤æ°ã®åå
ã¢ã¸ã¥ã¼ã«ã·ã¹ãã
ããã import
/require
ãç¨ããã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã使ç¨ããªããªãããã®ã»ã¯ã·ã§ã³ãã¹ããããããã¨ãã§ãã¾ãã使ç¨ããå ´åãããã¤ãã®ç¹å¥ãªæé ã¨ãã³ããç¨æãã¦ãã¾ãã
ã¢ã¸ã¥ã¼ã«ã·ã¹ãã å ã®ãã¼ã«ã«ç»é²
ãããè¦ã¦ããã¨ãããã¨ã¯ãããããããªã㯠Babel 㨠Webpack ã®ãããªãã®ãç¨ãã¦ãã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã使ç¨ãã¦ãããã¨ã§ãããããããããªããããããã®ã³ã³ãã¼ãã³ãããã¡ã¤ã«ã¨ãã¦é
ç½®ãã components
ãã£ã¬ã¯ããªãä½æãããã¨ãæ¨å¥¨ãã¾ãã
ãã¼ã«ã«ç»é²ãããåã«ã使ãããã³ã³ãã¼ãã³ããã¨ã«ã¤ã³ãã¼ãããå¿
è¦ãããã¾ããä¾ãã°ãComponentB.js
ã¾ã㯠ComponentB.vue
ãã¡ã¤ã«ãä»®å®ãã¦:
import ComponentA from './ComponentA'
import ComponentC from './ComponentC'
export default {
components: {
ComponentA,
ComponentC
},
// ...
}
ãã㧠ComponentA
㨠ComponentC
ã®ä¸¡æ¹ã ComponentB
ã®ãã³ãã¬ã¼ãå
ã§ä½¿ããããã«ãªãã¾ãã
åºåºã³ã³ãã¼ãã³ãã®èªåã°ãã¼ãã«ç»é²
ã³ã³ãã¼ãã³ãã®ãã¡å¤ãã¯æ¯è¼çå ±éãã¦ã input ã button ã®ãããªè¦ç´ ãã©ããããã ãã§ããæã«ãããã åºåºã³ã³ãã¼ãã³ã ã¨å¼ã³ããããã¯è¤æ°ã®ã³ã³ãã¼ãã³ãã横æãã¦é »ç¹ã«ç¨ãããã¾ãã
å¤æ°ã®ã³ã³ãã¼ãã³ããå¤æ°ã®åºåºã³ã³ãã¼ãã³ããå«ããçµæ:
import BaseButton from './BaseButton.vue'
import BaseIcon from './BaseIcon.vue'
import BaseInput from './BaseInput.vue'
export default {
components: {
BaseButton,
BaseIcon,
BaseInput
}
}
ãã³ãã¬ã¼ãã§ã¯ãæ¯è¼çå°ãªããã¼ã¯ã¢ããããµãã¼ããã¾ã:
<BaseInput
v-model="searchText"
@keydown.enter="search"
/>
<BaseButton @click="search">
<BaseIcon name="search"/>
</BaseButton>
幸ãã«ããWebpack (ã¾ã㯠å
é¨çã« Webpack ãå©ç¨ãã¦ãã Vue CLI 3+ ) ã使ç¨ãã¦ãããªãã ãã®ãããªé常ã«æ±ç¨çãªåºåºã³ã³ãã¼ãã³ãã®ã°ãã¼ãã«ç»é²ã« require.context
ãç¨ãããã¨ãã§ãã¾ãã次ã«ç¤ºãä¾ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ã¨ã³ããªãã¡ã¤ã«(ä¾: src/main.js
)ã§ãåºåºã³ã³ãã¼ãã³ããã°ãã¼ãã«ã«ã¤ã³ãã¼ãããã³ã¼ãã§ã:
import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
const requireComponent = require.context(
// ã³ã³ãã¼ãã³ããã©ã«ãã®ç¸å¯¾ãã¹
'./components',
// ãµããã©ã«ãå
ã調ã¹ããã©ãã
false,
// åºåºã³ã³ãã¼ãã³ãã®ãã¡ã¤ã«åã«ä¸è´ãããã®ã«ä½¿ãæ£è¦è¡¨ç¾
/Base[A-Z]\w+\.(vue|js)$/
)
requireComponent.keys().forEach(fileName => {
// ã³ã³ãã¼ãã³ãè¨å®ãåå¾ãã
const componentConfig = requireComponent(fileName)
// ã³ã³ãã¼ãã³ãåããã¹ã«ã«ã±ã¼ã¹ (PascalCase) ã§åå¾ãã
const componentName = upperFirst(
camelCase(
// ãã©ã«ãã®æ·±ãã«é¢ããããã¡ã¤ã«åãåå¾ãã
fileName
.split('/')
.pop()
.replace(/\.\w+$/, '')
)
)
// ã³ã³ãã¼ãã³ããã°ãã¼ãã«ç»é²ãã
Vue.component(
componentName,
// `export default` ã使ã£ã¦ã³ã³ãã¼ãã³ããã¨ã¯ã¹ãã¼ããããå ´åã«åå¨ãã
// `.default` ã§ã³ã³ãã¼ãã³ããªãã·ã§ã³ãæå¾
ãã¦ãã¦
// åå¨ããªãå ´åã«ã¯ã¢ã¸ã¥ã¼ã«ã®ã«ã¼ãã«ãã©ã¼ã«ããã¯ãã¾ãã
componentConfig.default || componentConfig
)
})
(new Vue
ã使ã£ã¦)ã«ã¼ã Vue ã¤ã³ã¹ã¿ã³ã¹ãä½æããããåã«ãã°ãã¼ãã«ç»é²ãè¡ãå¿
è¦ããããã¨ã è¦ãã¦ããã¦ãã ããããã®ä¾ã¯å®éã®ããã¸ã§ã¯ãã®æèã«ãããããã®ãã¿ã¼ã³ã®ä¸ä¾ã§ãã