ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- 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.js æ¬ä½ã§åºè·ããããã£ã¬ã¯ãã£ãã®æ¨æºã»ããã«å ã㦠(v-model
㨠v-show
)ãã«ã¹ã¿ã ãã£ã¬ã¯ãã£ã (custom directive) ãç»é²ãããã¨ãã§ãã¾ããVue 2.0 ã§ã¯ãã³ã¼ãã®åå©ç¨ã¨æ½è±¡åã«ãããåºæ¬ã®å½¢ã¯ã³ã³ãã¼ãã³ãã§ããããããªãããé常ã®è¦ç´ ã§ä½ã¬ãã« DOM ã«ã¢ã¯ã»ã¹ããªããã°ãªããªãã±ã¼ã¹ãããããããã¾ããããããã£ãå ´é¢ã§ã¯ãã«ã¹ã¿ã ãã£ã¬ã¯ãã£ããå½¹ç«ã¤ã§ããããã²ã¨ã¤ã®ä¾ã¨ãã¦ã以ä¸ã®ãã㪠input è¦ç´ ã¸ã®ãã©ã¼ã«ã¹ãæãããã¾ã:
ãã¼ã¸ãèªã¿è¾¼ãã¨ããã®è¦ç´ ã¯ãã©ã¼ã«ã¹ãæã«å ¥ãã¾ããå®éããã®ãã¼ã¸ã«è¨ªãã¦ããä»ã®ã¨ãããã¯ãªãã¯ãã¦ããªããã°ããã® input ã«ãã©ã¼ã«ã¹(注æ:ã¢ãã¤ã« Safari ã§ã¯èªåã§ãã©ã¼ã«ã¹ãã¾ãã)ãå½ãã£ã¦ããã§ããããããããããå®ç¾ããããã£ã¬ã¯ãã£ããä½ãã¾ãããã
// `v-focus` ã¨ããã°ãã¼ãã«ã«ã¹ã¿ã ãã£ã¬ã¯ãã£ããç»é²ãã¾ã
Vue.directive('focus', {
// ã²ãä»ãã¦ããè¦ç´ ã DOM ã«æ¿å
¥ãããæ...
inserted: function (el) {
// è¦ç´ ã«ãã©ã¼ã«ã¹ãå½ã¦ã
el.focus()
}
})
代ããã«ãã¼ã«ã«ãã£ã¬ã¯ãã£ãã«ç»é²ããããªãã°ãã³ã³ãã¼ãã³ãã® directives
ãªãã·ã§ã³ã§ç»é²ã§ãã¾ã:
directives: {
focus: {
// ãã£ã¬ã¯ãã£ãå®ç¾©
inserted: function (el) {
el.focus()
}
}
}
ããã§ãã³ãã¬ã¼ãã§ã以ä¸ã®ãããªæ°ãã v-focus
å±æ§ã使ããããã«ãªãã¾ãã
<input v-focus>
ããã¯é¢æ°
directive definition object ã¯ããã¤ãã®ããã¯é¢æ°(å ¨ã¦ä»»æ)ãæä¾ãã¾ã:
bind
: ãã£ã¬ã¯ãã£ããåãã¦å¯¾è±¡ã®è¦ç´ ã«ã²ãä»ããæã« 1 度ã ãå¼ã°ãã¾ãããã㧠1 åã ãå®è¡ããã»ããã¢ããå¦çãè¡ãã¾ããinserted
: ã²ãä»ãã¦ããè¦ç´ ã親 Node ã«æ¿å ¥ãããæã«å¼ã°ãã¾ã(ããã¯ã親 Node ãåå¨ãã¦ããæã«ã ãä¿è¨¼ãã¾ããå¿ ããããããã¥ã¡ã³ãã«ããã¨ã¯éãã¾ãã)ãupdate
: ã²ãä»ããè¦ç´ ãæ±åãã¦ããã³ã³ãã¼ãã³ãã® VNode ãæ´æ°ããã度ã«å¼ã°ãã¾ããããããããããåã³ã³ãã¼ãã³ããæ´æ°ãããåã§ãããã ãã£ã¬ã¯ãã£ãã®å¤ãå¤åãã¦ãããªãã¦ãããã¤ã³ãã£ã³ã°ããã¦ããå¤ã¨ä»¥åã®å¤ã¨ã®æ¯è¼ã«ãã£ã¦ä¸è¦ãªæ´æ°ãåé¿ãããã¨ãã§ãã¾ãã(ããã¯å¼æ°ã«é¢ãã¦ã¯ä¸è¨ãåç §ãã¦ãã ãã)VNodes ã«ã¤ãã¦ã¯ ãã¨ã§ 詳細ã«æ±ãã¾ãã æç»é¢æ° ã説æããã¨ãã§ãã
componentUpdated
: æ±åãã¦ããã³ã³ãã¼ãã³ãã® VNode ã¨åã³ã³ãã¼ãã³ãã® VNode ãæ´æ°ãããå¾ã«å¼ã°ãã¾ããunbind
: ãã£ã¬ã¯ãã£ããã²ãä»ãã¦ããè¦ç´ ããåãé¤ãããæã« 1 度ã ãå¼ã°ãã¾ãã
次ã®ã»ã¯ã·ã§ã³ã§ããããã®ããã¯ã«æ¸¡ãå¼æ°(ããªãã¡ el
, binding
, vnode
, oldVnode
)ãè¦ã¦ããã¾ãããã
ãã£ã¬ã¯ãã£ãããã¯å¼æ°
ãã£ã¬ã¯ãã£ãããã¯ã«ã¯ä»¥ä¸ã®å¼æ°ã渡ãã¾ã:
el
: ãã£ã¬ã¯ãã£ããã²ãä»ãè¦ç´ ãDOM ãç´æ¥æä½ããããã«ä½¿ç¨ã§ãã¾ããbinding
: 以ä¸ã®ããããã£ãå«ãã§ãããªãã¸ã§ã¯ããname
:v-
æ¥é è¾ (prefix) ç¡ãã®ãã£ã¬ã¯ãã£ãåãvalue
: ãã£ã¬ã¯ãã£ãã«æ¸¡ãããå¤ãä¾ãã°v-my-directive="1 + 1"
ã§ã¯ãvalue ã¯2
ã§ããoldValue
:update
ã¨componentUpdated
ã«ããã¦ã®ã¿å©ç¨ã§ãã以åã®å¤ãå¤ãå¤åãããã©ããã«é¢ãããå©ç¨ã§ãã¾ããexpression
: æååã¨ãã¦ã®ãã¤ã³ãã£ã³ã°å¼ãä¾ãã°v-my-directive="1 + 1"
ã§ã¯ãå¼ã¯"1 + 1"
ã§ããarg
: ããããã°ããã£ã¬ã¯ãã£ãã«æ¸¡ãããå¼æ°ãä¾ãã°v-my-directive:foo
ã§ã¯ãarg ã¯"foo"
ã§ããmodifiers
: ããããã°ã修飾å (modifier) ãå«ãã§ãããªãã¸ã§ã¯ããä¾ãã°v-my-directive.foo.bar
ã§ã¯ãmodifiers ãªãã¸ã§ã¯ãã¯{ foo: true, bar: true }
ã§ãã
vnode
: Vue ã®ã³ã³ãã¤ã©ã«ãã£ã¦çæãããä»®æ³ãã¼ããããã«è©³ãã㯠VNode API ãåç §ãã¦ãã ãããoldVnode
:update
ã¨componentUpdated
ããã¯ã«ããã¦ã®ã¿å©ç¨ã§ãã以åã®ä»®æ³ãã¼ãã
el
ãé¤ãã¦ããããã®å
¨ã¦ã®ããããã£ã¯èªã¿è¾¼ã¿ã®ã¿ (read-only) ã§å¤æ´ããªããã®ã¨ãã¦æ±ããªãã¦ã¯ããã¾ãããããã¯ãè¶
ãã¦ãã¼ã¿ãå
±æããå¿
è¦ãããå ´åã¯, è¦ç´ ã® dataset ãéãã¦è¡ããã¨ãæ¨å¥¨ããã¦ãã¾ãã
ããã¤ãã®ããããã£ã使ç¨ããã«ã¹ã¿ã ãã£ã¬ã¯ãã£ãã®ä¾:
<div id="hook-arguments-example" v-demo:foo.a.b="message"></div>
Vue.directive('demo', {
bind: function (el, binding, vnode) {
var s = JSON.stringify
el.innerHTML =
'name: ' + s(binding.name) + '<br>' +
'value: ' + s(binding.value) + '<br>' +
'expression: ' + s(binding.expression) + '<br>' +
'argument: ' + s(binding.arg) + '<br>' +
'modifiers: ' + s(binding.modifiers) + '<br>' +
'vnode keys: ' + Object.keys(vnode).join(', ')
}
})
new Vue({
el: '#hook-arguments-example',
data: {
message: 'hello!'
}
})
åçãªãã£ã¬ã¯ãã£ãå¼æ°
ãã£ã¬ã¯ãã£ãã®å¼æ°ã¯åçã«ã§ãã¾ããä¾ãã°ãv-mydirective:[argument]="value"
ã«ããã¦ãargument
ã¯ã³ã³ãã¼ãã³ãã¤ã³ã¹ã¿ã³ã¹ã® data ããããã£ã«åºã¥ãã¦æ´æ°ããã¾ãï¼ããã«ãããã¢ããªã±ã¼ã·ã§ã³å
ã§ã®ã«ã¹ã¿ã ãã£ã¬ã¯ãã£ãã®å©ç¨ãæè»ã«ãªãã¾ãã
è¦ç´ ããã¼ã¸ã®åºå®ä½ç½®ã«ãã³çãããã«ã¹ã¿ã ãã£ã¬ã¯ãã£ããä½ãããã¨ãã¾ãããã縦æ¹åã®ãã¯ã»ã«ä½ç½®ãå¤ã§è¨å®ããã«ã¹ã¿ã ãã£ã¬ã¯ãã£ãã次ã®ããã«ä½ããã¨ãã§ãã¾ã:
<div id="baseexample">
<p>Scroll down the page</p>
<p v-pin="200">Stick me 200px from the top of the page</p>
</div>
Vue.directive('pin', {
bind: function (el, binding, vnode) {
el.style.position = 'fixed'
el.style.top = binding.value + 'px'
}
})
new Vue({
el: '#baseexample'
})
ããã«ããããã¼ã¸ã®ä¸ç«¯ãã 200px ã®ä½ç½®ã«è¦ç´ ãåºå®ã§ãã¾ããããããä¸ç«¯ããã§ã¯ãªã左端ããã®ä½ç½®ã§è¦ç´ ããã³çãããããªã£ããã©ãã§ããããï¼åçå¼æ°ã¯ã³ã³ãã¼ãã³ãã®ã¤ã³ã¹ã¿ã³ã¹æ¯ã«æ´æ°ã§ããã®ã§ãããããå ´åã«å½¹ç«ã¡ã¾ã:
<div id="dynamicexample">
<h3>Scroll down inside this section â</h3>
<p v-pin:[direction]="200">I am pinned onto the page at 200px to the left.</p>
</div>
Vue.directive('pin', {
bind: function (el, binding, vnode) {
el.style.position = 'fixed'
var s = (binding.arg == 'left' ? 'left' : 'top')
el.style[s] = binding.value + 'px'
}
})
new Vue({
el: '#dynamicexample',
data: function () {
return {
direction: 'left'
}
}
})
çµæ:
ãã®ã«ã¹ã¿ã ãã£ã¬ã¯ãã£ãã¯ãã¡ãã£ã¨ããéãã®ã¦ã¼ã¹ã±ã¼ã¹ãæè»ã«ãµãã¼ãã§ããããã«ãªãã¾ããã
é¢æ°ã«ããçç¥è¨æ³
å¤ãã®å ´åãbind
㨠update
ã«ã¯åãæ¯èãã欲ããã§ããããããã®ä»ã®ããã¯ã«é¢ãã¦ã¯æ°ã«ããã¾ãããä¾ãã°:
Vue.directive('color-swatch', function (el, binding) {
el.style.backgroundColor = binding.value
})
ãªãã¸ã§ã¯ããªãã©ã«
ããªãã®ãã£ã¬ã¯ãã£ããè¤æ°ã®å¤ãå¿ è¦ãªãã°ãJavaScript ãªãã¸ã§ã¯ããªãã©ã«ã渡ããã¨ãã§ãã¾ãããã£ã¬ã¯ãã£ãã¯ä»»æã®å¦¥å½ãª JavaScript å¼ãåããã¨ãã§ããã®ãè¦ãã¦ããã¦ãã ãã:
<div v-demo="{ color: 'white', text: 'hello!' }"></div>
Vue.directive('demo', function (el, binding) {
console.log(binding.value.color) // => "white"
console.log(binding.value.text) // => "hello!"
})