ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- 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å¹´4æ8æ¥
ç®åºããããã£
ãã³ãã¬ã¼ãå ã«å¼ãæ¸ããã®ã¯ã¨ã¦ã便å©ã§ãããé常ã«ç°¡åãªæä½ããã§ãã¾ããããã³ãã¬ã¼ãå ã«å¤ãã®ãã¸ãã¯ãè©°ãè¾¼ãã¨ãã³ã¼ããè¥å¤§åããã¡ã³ããã³ã¹ãé£ãããªãã¾ããä¾ãã°:
<div id="example">
{{ message.split('').reverse().join('') }}
</div>
ãããªã£ã¦ããã¨ããã³ãã¬ã¼ãã¯ã·ã³ãã«ã§ã宣è¨çã§ããªããªã£ã¦ãã¾ã£ã¦ãã¾ãããã°ããçºãã¦ããã£ã¨ããã message
ãéã«ãã¦è¡¨ç¤ºãã¦ãããã¨ã«æ°ä»ãã§ããããéã«ããã¡ãã»ã¼ã¸ããã³ãã¬ã¼ãã®ä¸ã§ 2 å以ä¸ä½¿ããã¨ããã¨ãåé¡ã¯ããæ·±å»ã«ãªãã¾ãã
ä¸è¨ã®çç±ãããè¤éãªãã¸ãã¯ã«ã¯ç®åºããããã£ãå©ç¨ãã¹ãã§ãã
åºæ¬çãªä¾
<div id="example">
<p>Original message: "{{ message }}"</p>
<p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>
var vm = new Vue({
el: '#example',
data: {
message: 'Hello'
},
computed: {
// ç®åº getter é¢æ°
reversedMessage: function () {
// `this` 㯠vm ã¤ã³ã¹ã¿ã³ã¹ãæãã¾ã
return this.message.split('').reverse().join('')
}
}
})
çµæ:
Original message: "{{ message }}"
Computed reversed message: "{{ reversedMessage }}"
ããã§ã¯ãç®åºãããã㣠reversedMessage
ã宣è¨ãã¾ãããç§ãã¡ãæä¾ããæ©è½ã¯ããããã㣠vm.reversedMessage
ã«å¯¾ãã getter é¢æ°ã¨ãã¦å©ç¨ããã¾ã:
console.log(vm.reversedMessage) // => 'olleH'
vm.message = 'Goodbye'
console.log(vm.reversedMessage) // => 'eybdooG'
ã³ã³ã½ã¼ã«ãéãã¦ãvm ã§éãã§ã¿ã¦ãã ãããvm.reversedMessage
ã®å¤ã¯ã常㫠vm.message
ã®å¤ã«ä¾åãã¦ãã¾ãã
é常ã®ããããã£ã¨åãããã«ããã³ãã¬ã¼ãå
ã®ç®åºããããã£ã«ãã¼ã¿ãã¤ã³ããããã¨ãã§ãã¾ããVue 㯠vm.reversedMessage
ã vm.message
ã«ä¾åãã¦ãããã¨ãç¥ã£ã¦ããã®ã§ãvm.message
ãå¤ãã㨠vm.reversedMessage
ã«ä¾åããå
¨ã¦ã®ãã¤ã³ãã£ã³ã°ãæ´æ°ãã¾ããããã«ãæãè¯ãã¨ããã¯ããã®ä¾åé¢ä¿ã宣è¨çã«ä½æããã¦ãããã¨ã§ããç®åº getter é¢æ°ã¯å¯ä½ç¨ããªãã®ã§ããã¹ããå¤ã®æ¨è«ã容æã«ãªãã¾ãã
ç®åºãããã㣠vs ã¡ã½ãã
ãããã£ãå¼ãæã¤ã¡ã½ãããå¼ã³åºããã¨ã§ãåãçµæãå®ç¾ã§ãããã¨ã«æ°ä»ããããããã¾ãã:
<p>Reversed message: "{{ reverseMessage() }}"</p>
// ã³ã³ãã¼ãã³ãå
methods: {
reverseMessage: function () {
return this.message.split('').reverse().join('')
}
}
ç®åºããããã£ã®ä»£ããã«ãåããããªé¢æ°ãã¡ã½ããã¨ãã¦å®ç¾©ãããã¨ãå¯è½ã§ããæçµçã«ã¯ã2ã¤ã®ã¢ããã¼ãã¯å®å
¨ã«åãçµæã«ãªãã¾ããããããªãããç®åºããããã£ã¯ãªã¢ã¯ãã£ããªä¾åé¢ä¿ã«ãã¨ã¥ããã£ãã·ã¥ãããã¨ããéããããã¾ããç®åºããããã£ã¯ããªã¢ã¯ãã£ããªä¾åé¢ä¿ãæ´æ°ãããã¨ãã«ã ãåè©ä¾¡ããã¾ããããã¯ã¤ã¾ããmessage
ãå¤ãããªãéãã¯ãreversedMessage
ã«ä½åº¦ã¢ã¯ã»ã¹ãã¦ããé¢æ°ãåã³å®è¡ãããã¨ãªã以åè¨ç®ãããçµæãå³æã«è¿ãã¨ãããã¨ã§ãã
Date.now()
ã¯ãªã¢ã¯ãã£ããªä¾åã§ã¯ãªãããã次ã®ç®åºããããã£ã¯äºåº¦ã¨æ´æ°ãããªããã¨ãæå³ãã¾ã:
computed: {
now: function () {
return Date.now()
}
}
対称çã«ãã¡ã½ããå¼ã³åºãã¯ãåæç»ãèµ·ããã¨å¸¸ã«é¢æ°ãå®è¡ãã¾ãã
ãªããã£ãã·ã³ã°ãå¿ è¦ãªã®ã§ããããï¼å·¨å¤§ãªé åãã«ã¼ããããå¤ãã®è¨ç®ãå¿ è¦ã¨ãããã³ã¹ãã®é«ã A ã¨ããç®åºããããã£ããããã¨ãæ³åãã¦ã¿ã¦ãã ãããA ã«ä¾åããä»ã®ç®åºããããã£ãããããããã¾ããããã®å ´åããã£ãã·ã³ã°ããªããã°å¿ è¦ä»¥ä¸ã« A ã® getter ãå®è¡ãããã¨ã«ãªã£ã¦ãã¾ãã¾ãããã£ãã·ã³ã°ããããªãå ´åã¯ã代ããã«ã¡ã½ããã使ãã¾ãããã
ç®åºãããã㣠vs ç£è¦ããããã£
Vue 㯠Vue ã¤ã³ã¹ã¿ã³ã¹ä¸ã®ãã¼ã¿ã®å¤æ´ãç£è¦ãåå¿ããããã¨ãã§ãããããæ±ç¨ç㪠ç£è¦ããããã£(watched property) ãæä¾ãã¦ãã¾ããä»ã®ãã¼ã¿ã«åºã¥ãã¦å¤æ´ããå¿
è¦ããããã¼ã¿ãããå ´åãç¹ã« AngularJS ã«æ
£ãã¦ããããwatch
ãå¤ãå©ç¨ãããã¨æãããããã¾ãããããããå½ä»¤ç㪠watch
ã³ã¼ã«ããã¯ããããå¤ãã®å ´åã§ã¯ç®åºããããã£ãå©ç¨ããã»ããè¯ãã§ãããã次ã®ä¾ã§èãã¦ã¿ã¾ããã:
<div id="demo">{{ fullName }}</div>
var vm = new Vue({
el: '#demo',
data: {
firstName: 'Foo',
lastName: 'Bar',
fullName: 'Foo Bar'
},
watch: {
firstName: function (val) {
this.fullName = val + ' ' + this.lastName
},
lastName: function (val) {
this.fullName = this.firstName + ' ' + val
}
}
})
ä¸è¨ã®ã³ã¼ãã¯å½ä»¤çã§åé·ã§ããç®åºããããã£ãå©ç¨ãããã¼ã¸ã§ã³ã¨æ¯è¼ãã¦ã¿ã¾ããã:
var vm = new Vue({
el: '#demo',
data: {
firstName: 'Foo',
lastName: 'Bar'
},
computed: {
fullName: function () {
return this.firstName + ' ' + this.lastName
}
}
})
ãã¡ãã®æ¹ããã¯ããã«è¯ãããã¾ãããï¼
ç®åº Setter é¢æ°
ç®åºããããã£ã¯ããã©ã«ãã§ã¯ getter é¢æ°ã®ã¿ã§ãããå¿ è¦ãããã° setter é¢æ°ã使ãã¾ã:
// ...
computed: {
fullName: {
// getter é¢æ°
get: function () {
return this.firstName + ' ' + this.lastName
},
// setter é¢æ°
set: function (newValue) {
var names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[names.length - 1]
}
}
}
// ...
vm.fullName = 'John Doe'
ãå¼ã¶ã¨ãsetter é¢æ°ãå¼ã³åºãããvm.firstName
㨠vm.lastName
ãé©åã«æ´æ°ããã¾ãã
ã¦ã©ããã£
å¤ãã®å ´åã§ã¯ç®åºããããã£ã®æ¹ãé©åã§ã¯ããã¾ãããã«ã¹ã¿ã ã¦ã©ããã£ãå¿
è¦ãªæãããã§ãããããã¼ã¿ã®å¤æ´ã«å¯¾ãã¦åå¿ãããããæ±ç¨ç㪠watch
ãªãã·ã§ã³ã Vue ãæä¾ãã¦ããã®ã¯ãã®ããã§ãããã¼ã¿ãå¤ããã®ã«å¿ãã¦éåæãã³ã¹ãã®é«ãå¦çãå®è¡ãããã¨ãã«æã便å©ã§ãã
ä¾:
<div id="watch-example">
<p>
Ask a yes/no question:
<input v-model="question">
</p>
<p>{{ answer }}</p>
</div>
<!-- ajax ã©ã¤ãã©ãªã®è±å¯ãªã¨ã³ã·ã¹ãã ããæ±ç¨çãªã¦ã¼ãã£ãªã㣠-->
<!-- ã¡ã½ãããããããããã®ã§ãVue ã®ã³ã¢ã¯ããããåçºæããã« -->
<!-- å°ããä¿ããã¦ãã¾ãããã®çµæã¨ãã¦ãæ
£ã親ããã§ãããã®ã ãã -->
<!-- 使ãããããªèªç±ãã Vue ã¯æã¡åããã¦ãã¾ãã -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script>
var watchExampleVM = new Vue({
el: '#watch-example',
data: {
question: '',
answer: 'I cannot give you an answer until you ask a question!'
},
watch: {
// ãã®é¢æ°ã¯ question ãå¤ãããã¨ã«å®è¡ããã¾ãã
question: function (newQuestion, oldQuestion) {
this.answer = 'Waiting for you to stop typing...'
this.debouncedGetAnswer()
}
},
created: function () {
// _.debounce ã¯ç¹ã«ã³ã¹ãã®é«ãå¦çã®å®è¡ãå¶å¾¡ããããã®
// lodash ã®é¢æ°ã§ãããã®å ´åã¯ãã©ã®ãããé »ç¹ã« yesno.wtf/api
// ã¸ã®ã¢ã¯ã»ã¹ãã¹ãããå¶éããããã«ãã¦ã¼ã¶ã¼ã®å
¥åãå®å
¨ã«
// çµããã®ãå¾
ã£ã¦ãã ajax ãªã¯ã¨ã¹ããå®è¡ãã¦ãã¾ãã
// _.debounce (ã¨ãã®è¦ªæã§ãã _.throttle ) ã«ã¤ãã¦ã®è©³ç´°ã¯
// https://lodash.com/docs#debounce ãè¦ã¦ãã ããã
this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
},
methods: {
getAnswer: function () {
if (this.question.indexOf('?') === -1) {
this.answer = 'Questions usually contain a question mark. ;-)'
return
}
this.answer = 'Thinking...'
var vm = this
axios.get('https://yesno.wtf/api')
.then(function (response) {
vm.answer = _.capitalize(response.data.answer)
})
.catch(function (error) {
vm.answer = 'Error! Could not reach the API. ' + error
})
}
}
})
</script>
çµæ:
Ask a yes/no question:
{{ answer }}
ãã®å ´åã§ã¯ãwatch
ãªãã·ã§ã³ãå©ç¨ãããã¨ã§ãéåæå¦ç( API ã®ã¢ã¯ã»ã¹)ã®å®è¡ããå¦çãã©ã®ãããã®é »åº¦ã§å®è¡ããããå¶å¾¡ããããæçµç㪠answer ãåå¾ã§ããã¾ã§ã¯ä¸éã®ç¶æ
ã«ãã¦ãããã¨ãã£ããã¨ãå¯è½ã«ãªã£ã¦ãã¾ãããããã¯ããããç®åºããããã£ã§ã¯å®ç¾ã§ãã¾ããã
watch
ãªãã·ã§ã³ã«å ãã¦ãå½ä»¤ç㪠vm.$watch API ãå©ç¨ãããã¨ãã§ãã¾ãã