ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- 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å¹´3æ31æ¥
ããã«æ·±ãè¦ã¦ããã¾ãããï¼Vue ã®æ大ã®ç¹å¾´ã®1ã¤ã¯ãæ§ãããªãªã¢ã¯ãã£ãã·ã¹ãã ã§ããã¢ãã«ã¯åãªããã¬ã¼ã³ãª JavaScript ãªãã¸ã§ã¯ãã§ããããããå¤æ´ããã¨ãã¥ã¼ãæ´æ°ããã¾ããããã¯ç¶æ 管çãé常ã«ã·ã³ãã«ãã¤ç´æçã«ãã¾ãããããããåé¡ãé¿ããããã«ãã®ä»çµã¿ãç解ãããã¨ãéè¦ã§ãããã®ã»ã¯ã·ã§ã³ã§ã¯ãVue ã®ãªã¢ã¯ãã£ãã·ã¹ãã ã®ä½ã¬ãã«ã®è©³ç´°ã®ä¸é¨ã«ã¤ãã¦æãä¸ãã¦ããã¾ãã
å¤æ´ã®è¿½è·¡æ¹æ³
ãã¬ã¼ã³ãª JavaScript ãªãã¸ã§ã¯ãã data
ãªãã·ã§ã³ã¨ã㦠Vue ã¤ã³ã¹ã¿ã³ã¹ã«æ¸¡ãã¨ããVue ã¯ãã®å
¨ã¦ã®ããããã£ã渡ãæ©ãã¦ããããã Object.defineProperty
ã使ç¨ã㦠getter/setter ã«å¤æãã¾ãããã㯠ES5 ã ãã®ãã·ã (shim) ãã§ããªãæ©è½ã§ãVue ã IE8 以ä¸ããµãã¼ãããªãã®ã¯ãã®ããã§ãã
getter/setter ã¯ã¦ã¼ã¶ã¼ã«ã¯è¦ãã¾ããããå é¨ã§ã¯ãVue ãä¾åæ§ã追跡ããããããããã£ãã¢ã¯ã»ã¹ã¾ãã¯å¤æ´ãããã¨ãã«å¤æ´ãéç¥ããããããã¨ãå¯è½ã«ãã¦ãã¾ããã²ã¨ã¤æ³¨æç¹ãæããã¨ãå¤æããããã¼ã¿ãªãã¸ã§ã¯ãããã°åºåããã¨ããåãã©ã¦ã¶ã³ã³ã½ã¼ã«ã§ getter/setter ã®ãã©ã¼ããããç°ãªãããããã調æ»ã«é©ããã¤ã³ã¿ã¼ãã§ã¤ã¹ã® vue-devtools ãã¤ã³ã¹ãã¼ã«ããã»ããããããããã¾ããã
å ¨ã¦ã®ã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ã¯å¯¾å¿ãã ã¦ã©ãã㣠(watcher) ã¤ã³ã¹ã¿ã³ã¹ãæã£ã¦ãã¦ãããã¯ã³ã³ãã¼ãã³ããæç»ããéã« â触ãã (touched)â ããããã£ãå ¨ã¦ä¾åæ§ã¨ãã¦è¨é²ãã¦ãã¾ãããã®å¾ãä¾åæ§ã® setter ãããªã¬ãããã¨ãã¦ã©ããã£ã«éç¥ãã¦ã³ã³ãã¼ãã³ãã®åæç»ãèµ·åãã¾ãã
å¤æ´æ¤åºã®æ³¨æäºé
JavaScript ã®å¶éã®ãããVue ã¯ãæ¤åºãããã¨ãã§ããªãå¤æ´ã®ã¿ã¤ããããã¾ããããããããããåé¿ããªã¢ã¯ãã£ããã£ãç¶æããæ¹æ³ã¯ããã¾ãã
ãªãã¸ã§ã¯ãã«é¢ãã¦
Vue ã¯ããããã£ã®è¿½å ã¾ãã¯åé¤ãæ¤åºã§ãã¾ãããVue ã¯ã¤ã³ã¹ã¿ã³ã¹ã®åæåä¸ã« getter/setter ã®å¤æãè¡ããããå
¨ã¦ã®ããããã£ã¯ Vue ãå¤æãã¦ãªã¢ã¯ãã£ãã«ã§ããããã« data
ãªãã¸ã§ã¯ãã«åå¨ããªããã°ãªãã¾ãããä¾ãã°:
var vm = new Vue({
data: {
a: 1
}
})
// `vm.a` ã¯ä»ãªã¢ã¯ãã£ãã§ã
vm.b = 2
// `vm.b` ã¯ãªã¢ã¯ãã£ãã§ã¯"ããã¾ãã"
Vue ã§ã¯ããã§ã«ä½æãããã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ãã¦æ°ããã«ã¼ãã¬ãã«ã®ãªã¢ã¯ãã£ããªããããã£ãåçã«è¿½å ãããã¨ã¯ã§ãã¾ãããããããªãããVue.set(object, propertyName, value)
ã¡ã½ããã使ã£ã¦ãã¹ããããªãã¸ã§ã¯ãã«ãªã¢ã¯ãã£ããªããããã£ã追å ãããã¨ãã§ãã¾ã:
Vue.set(vm.someObject, 'b', 2)
vm.$set
ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã使ç¨ãããã¨ãã§ãã¾ããããã¯ã°ãã¼ãã«ã® Vue.set
ã®ã¨ã¤ãªã¢ã¹ã§ã:
this.$set(this.someObject, 'b', 2)
æ¢åã®ãªãã¸ã§ã¯ãã«è¤æ°ã®ããããã£ãå²ãå½ã¦ããã¨ãããã¨ãããããããã¾ãããä¾ãã°ãObject.assign()
ã _.extend()
ã使ã£ã¦ããããããªãã¸ã§ã¯ãã«è¿½å ãããæ°ããããããã£ã¯å¤æ´ãããªã¬ãã¾ããããã®ãããªå ´åã¯ãå
ã®ãªãã¸ã§ã¯ãã¨ããã¯ã¹ã¤ã³ãªãã¸ã§ã¯ãã®ä¸¡æ¹ã®ããããã£ãæã¤æ°ããªãªãã¸ã§ã¯ããä½æãã¦ãã ãã:
// `Object.assign(this.someObject, { a: 1, b: 2 })` ã®ä»£ãã
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })
é åã«é¢ãã¦
Vue ã¯ãé åã«ããã次ã®å¤æ´ã¯æ¤ç¥ã§ãã¾ãã:
- ã¤ã³ããã¯ã¹ã¨ä¸ç·ã«ã¢ã¤ãã ãç´æ¥ã»ããããå ´åãä¾ãã°
vm.items[indexOfItem] = newValue
- é
åã®é·ããå¤æ´ããå ´åãä¾ãã°
vm.items.length = newLength
ä¾ãã°:
var vm = new Vue({
data: {
items: ['a', 'b', 'c']
}
})
vm.items[1] = 'x' // is NOT reactive
vm.items.length = 2 // is NOT reactive
注æäºé
1 ãå
æããã«ã次ã®ãããã vm.items[indexOfItem] = newValue
ã¨åæ§ã«æ©è½ãã¾ããããªã¢ã¯ãã£ããã£ã·ã¹ãã ã§ç¶æ
ã®æ´æ°ãããªã¬ãã¾ã:
// Vue.set
Vue.set(vm.items, indexOfItem, newValue)
// Array.prototype.splice
vm.items.splice(indexOfItem, 1, newValue)
ã¾ããvm.$set
ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã使ããã¨ãã§ãã¾ããããã¯ã°ãã¼ãã«ãª Vue.set
ã®ã¨ã¤ãªã¢ã¹ã§ã:
vm.$set(vm.items, indexOfItem, newValue)
注æäºé
2 ã«å¯¾å¿ããã«ã¯ãsplice
ã使ããã¨ãã§ãã¾ãã
vm.items.splice(newLength)
ãªã¢ã¯ãã£ãããããã£ã®å®£è¨
Vue ã§ã¯æ°ããã«ã¼ãã¬ãã«ã®ãªã¢ã¯ãã£ããªããããã£ãåçã«è¿½å ãããã¨ã¯ã§ããªããããã¤ã³ã¹ã¿ã³ã¹ã®åæåæã«åãã£ã¦å ¨ã¦ã®ã«ã¼ãã¬ãã«ã®ãªã¢ã¯ãã£ã㪠data ããããã£ã宣è¨ããå¿ è¦ãããã¾ãã空ã®å¤ã§ããã¾ãã¾ãã:
var vm = new Vue({
data: {
// 空ã®å¤ã¨ã㦠message ã宣è¨ãã
message: ''
},
template: '<div>{{ message }}</div>'
})
// å¾ã§ã`message` ã追å ãã
vm.message = 'Hello!'
data ãªãã·ã§ã³ã§ message
ã宣è¨ãã¦ããªãã¨ãVue 㯠render ãã¡ã³ã¯ã·ã§ã³ãåå¨ããªãããããã£ã«ã¢ã¯ã»ã¹ãããã¨ãã¦ãããã¨ãè¦åãã¾ãã
ãã®å¶éã®èå¾ã«ã¯æè¡çãªçç±ãããã¾ããããã¯ä¾åæ§è¿½è·¡ã·ã¹ãã ã«ãããä¸é£ã®ã¨ãã¸ã±ã¼ã¹ãæé¤ããã¾ã Vue ã¤ã³ã¹ã¿ã³ã¹ã¨åãã§ãã¯ã·ã¹ãã ã¨ã®è¦ªåæ§ãé«ãã¾ãããããã³ã¼ãã®ä¿å®æ§ã®è¦³ç¹ãããéè¦ãªäºé
ãããã¾ããdata
ãªãã¸ã§ã¯ãã¯ã³ã³ãã¼ãã³ãã®ç¶æ
ã®ã¹ãã¼ãã®ãããªãã®ã§ããåãã£ã¦å
¨ã¦ã®ãªã¢ã¯ãã£ããªããããã£ã宣è¨ãã¦ããã¨ãå¾ããè¦ç´ãããå¥ã®éçºè
ãèªãã ãããã¨ãã«ã³ã³ãã¼ãã³ãã®ã³ã¼ããç°¡åã«ç解ãããã¨ãã§ãã¾ãã
éåææ´æ°ãã¥ã¼
ããããæ¢ã«ãæ°ã¥ãã§ãããããVue 㯠éåæ ã« DOM æ´æ°ãå®è¡ãã¾ãããã¼ã¿å¤æ´ãæ¤åºãããã¨ãVue ã¯ãã¥ã¼ããªã¼ãã³ããåãã¤ãã³ãã«ã¼ãã§èµ·ããå
¨ã¦ã®ãã¼ã¿å¤æ´ããããã¡ãªã³ã°ãã¾ããåãã¦ã©ããã£ãè¤æ°åããªã¬ãããå ´åããã¥ã¼ã«ã¯ä¸åº¦ã ãå
¥ãã¾ãããã®éè¤é¤å¤ãããã¡ãªã³ã°ã¯ä¸è¦ãªè¨ç®ã DOM æä½ãåé¿ããä¸ã§éè¦ã§ããããã¦ã次ã®ã¤ãã³ãã«ã¼ã âtickâ ã§ãVue ã¯ãã¥ã¼ããã©ãã·ã¥ããå®éã®(ãã§ã«éè¤ãé¤å¤ããã)ä½æ¥ãå®è¡ãã¾ããå
é¨çã«ã¯ãVue ã¯éåæãã¥ã¼ã¤ã³ã°åãã«ãã¤ãã£ã㪠Promise.then
㨠MutationObserver
ãsetImmediate
ãå©ç¨å¯è½ãªãããã使ããsetTimeout(fn, 0)
ã«ãã©ã¼ã«ããã¯ãã¾ãã
ä¾ã¨ãã¦ãvm.someData = 'new value'
ãã»ããããæããã®ã³ã³ãã¼ãã³ãã¯ããã«ã¯åæç»ãã¾ããã 次㮠âtickâ ã§ãã¥ã¼ããã©ãã·ã¥ãããæã«æ´æ°ãã¾ããã»ã¨ãã©ã®å ´åãç§éã¯ããã«ã¤ãã¦æ°ã«ããå¿
è¦ã¯ããã¾ããããæ´æ°ãã DOM ã®ç¶æ
ã«ä¾åããä½ãããããæã¯æ³¨æãå¿
è¦ã§ããVue.js ã¯ä¸è¬çã«âãã¼ã¿é§åâçãªæµåã§èããDOM ãç´æ¥è§¦ãã®ãé¿ãããã¨ãéçºè
ã«å¥¨å±ãã¾ãããæã«ã¯ãã®æãæ±ãå¿
è¦ãããããããã¾ããããã¼ã¿ã®å¤æ´å¾ã« Vue.js ã® DOM æ´æ°ã®å®äºãå¾
ã¤ã«ã¯ããã¼ã¿ãå¤æ´ãããç´å¾ã« Vue.nextTick(callback)
ã使ç¨ãããã¨ãã§ãã¾ãããã®ã³ã¼ã«ããã¯ã¯ DOM ãæ´æ°ãããå¾ã«å¼ã°ãã¾ããä¾ãã°:
<div id="example">{{ message }}</div>
var vm = new Vue({
el: '#example',
data: {
message: '123'
}
})
vm.message = 'new message' // ãã¼ã¿ãå¤æ´ãã
vm.$el.textContent === 'new message' // false
Vue.nextTick(function () {
vm.$el.textContent === 'new message' // true
})
vm.$nextTick()
ã¨ããã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããããã¾ããããã¯ãã°ãã¼ãã«ãª Vue ãå¿
è¦ã¨ãããã³ã¼ã«ããã¯ã® this
ã³ã³ããã¹ããèªåçã«ç¾å¨ã® Vue ã¤ã³ã¹ã¿ã³ã¹ã«æç¸ããããããã³ã³ãã¼ãã³ãå
ã§ç¹ã«å½¹ç«ã¡ã¾ã:
Vue.component('example', {
template: '<span>{{ message }}</span>',
data: function () {
return {
message: 'not updated'
}
},
methods: {
updateMessage: function () {
this.message = 'updated'
console.log(this.$el.textContent) // => 'not updated'
this.$nextTick(function () {
console.log(this.$el.textContent) // => 'updated'
})
}
}
})
$nextTick()
㯠Promise ãè¿å´ãããããæ°ãã ES2017 async/await æ§æãç¨ãã¦ãåããã¨ãã§ãã¾ã:
methods: {
updateMessage: async function () {
this.message = 'updated'
console.log(this.$el.textContent) // => 'not updated'
await this.$nextTick()
console.log(this.$el.textContent) // => 'updated'
}
}