ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- 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å¹´4æ17æ¥
ãã®ãã¼ã¸ã¯ãã³ã³ãã¼ãã³ãã®åºæ¬ãèªãã§ãããã¨ãåæã¨ãã¦ãã¾ããã¯ããã¦ã³ã³ãã¼ãã³ãã«ã¤ãã¦èªãå ´åã¯ãã¾ãã¯ãã¡ãããèªã¿ãã ããã
ããããã£ã®å½¢å¼ (ãã£ã¡ã«ã±ã¼ã¹ vs ã±ããã±ã¼ã¹)
HTML ã®å±æ§åã¯å¤§æåå°æåãåºå¥ããããã©ã¦ã¶ã¯å ¨ã¦ã®å¤§æåãå°æåã¨ãã¦è§£éãã¾ããã¤ã¾ãã¯ã DOM(HTML) ã®ãã³ãã¬ã¼ãå ã«ããã¦ã¯ããã£ã¡ã«ã±ã¼ã¹ã®ããããã£ã¯ã±ããã±ã¼ã¹(ãã¤ãã³ã§åºåã£ããã®)ã使ç¨ããå¿ è¦ãããã¾ãã
Vue.component('blog-post', {
// JavaScript å
ã§ã¯ãã£ã¡ã«ã±ã¼ã¹
props: ['postTitle'],
template: '<h3>{{ postTitle }}</h3>'
})
<!-- HTML å
ã§ã¯ã±ããã±ã¼ã¹ -->
<blog-post post-title="hello!"></blog-post>
æååãã³ãã¬ã¼ããå©ç¨ãã¦ããå ´åã¯ããã®å¶éã¯é©ç¨ããã¾ããã
ããããã£ã®å
ããã¾ã§ã¯ãããããã£ãæååã®é åã¨ãã¦åæãã¦ãã¾ããã
props: ['title', 'likes', 'isPublished', 'commentIds', 'author']
ããããªãããé常ã¯ããããã£ã®å¤ãç¹å®ã®åã«ãªããã¨ãæå¾ ããã§ãããããã®ãããªå ´åãããããã£ããªãã¸ã§ã¯ãã¨ãã¦åæãããã¨ãã§ãã¾ãããã®ãªãã¸ã§ã¯ãã®ãã¼ã¨å¤ã«ã¯ãããããããããã£åã¨åãè¨å®ãã¾ã:
props: {
title: String,
likes: Number,
isPublished: Boolean,
commentIds: Array,
author: Object,
callback: Function,
contactsPromise: Promise // or any other constructor
}
ãããããã¨ã§ã³ã³ãã¼ãã³ãã®èª¬æã«ãªãã ãã§ãªããééã£ãåã渡ããå ´åã«ããã©ã¦ã¶ã® JavaScript ã³ã³ã½ã¼ã«ã«ã¦è¦åããã¾ãã詳ããã¯ãã®ãã¼ã¸ã®ä¸ã«ããtype checks and other prop validations ã«ã¦èª¬æãã¾ãã
éçãããã¯åçãªããããã£ã®åã渡ã
ããã¾ã§ã¯ããã®ãããªå½¢ã§ããããã£ãéçãªå¤ã渡ãã¦ããã¨ãããè¦ã¦ãã¾ããã:
<blog-post title="My journey with Vue"></blog-post>
次ã®ãã㪠v-bind
ã§åçã«å²ãå½ã¦ãããããããã£ãè¦ã¦ãã¾ãã:
<!-- å¤æ°ã®å¤ãåçã«å²ãå½ã¦ã¾ã -->
<blog-post v-bind:title="post.title"></blog-post>
<!-- è¤æ°ã®å¤æ°ãåæããå¤ãåçã«å²ãå½ã¦ã¾ã -->
<blog-post
v-bind:title="post.title + ' by ' + post.author.name"
></blog-post>
ä¸ã® 2 ã¤ã®ä¾ã§ã¯æååã®å¤ã渡ãã¦ãã¾ãããããããã£ã«ã¯ ä»»æã® åã®å¤ã渡ããã¨ãå¯è½ã§ãã
æ°å¤ã®åã渡ã
<!-- `42` ã¯éçãªå¤ã§ããããããæååã§ã¯ãªã JavaScript ã®å¼ã 㨠-->
<!-- Vue ã«ä¼ããã«ã¯ v-bind ã使ãå¿
è¦ãããã¾ãã -->
<blog-post v-bind:likes="42"></blog-post>
<!-- å¤æ°ã®å¤ãåçã«å²ãå½ã¦ã¦ãã¾ãã -->
<blog-post v-bind:likes="post.likes"></blog-post>
çå½å¤ã®åã渡ã
<!-- å¤ã®ãªãããããã£ã¯ã `true` ãæå³ãããã¨ã¨ãªãã¾ãã -->
<blog-post is-published></blog-post>
<!-- `false` ã¯éçãªå¤ã§ããããããæååã§ã¯ãªã JavaScript ã®å¼ã 㨠-->
<!-- Vue ã«ä¼ããã«ã¯ã v-bind ã使ãå¿
è¦ãããã¾ãã -->
<blog-post v-bind:is-published="false"></blog-post>
<!-- å¤æ°ã®å¤ãåçã«å²ãå½ã¦ã¦ãã¾ãã -->
<blog-post v-bind:is-published="post.isPublished"></blog-post>
é åã®åã渡ã
<!-- ãã®é
åã¯éçã§ããããããæååã§ã¯ãªã JavaScript ã®å¼ã 㨠-->
<!-- Vue ã«ä¼ããã«ã¯ã v-bind ã使ãå¿
è¦ãããã¾ãã -->
<blog-post v-bind:comment-ids="[234, 266, 273]"></blog-post>
<!-- å¤æ°ã®å¤ãåçã«å²ãå½ã¦ã¦ãã¾ãã -->
<blog-post v-bind:comment-ids="post.commentIds"></blog-post>
ãªãã¸ã§ã¯ãã®åã渡ã
<!-- ãã®ãªãã¸ã§ã¯ãã¯éçã§ããããããæååã§ã¯ãªã JavaScript ã®å¼ã 㨠-->
<!-- Vue ã«ä¼ããã«ã¯ã v-bind ã使ãå¿
è¦ãããã¾ãã -->
<blog-post
v-bind:author="{
name: 'Veronica',
company: 'Veridian Dynamics'
}"
></blog-post>
<!-- å¤æ°ã®å¤ãåçã«å²ãå½ã¦ã¦ãã¾ãã -->
<blog-post v-bind:author="post.author"></blog-post>
ãªãã¸ã§ã¯ãã®ããããã£ã®åã渡ã
ãªãã¸ã§ã¯ãã®å
¨ã¦ã®ããããã£ãã³ã³ãã¼ãã³ãã®ããããã£(props)ã¨ãã¦æ¸¡ãããå ´åã¯ãv-bind
ãå¼æ°ç¡ãã§ä½¿ããã¨ãã§ãã¾ã(v-bind:prop-name
ã®ä»£ããã« v-bind
ã使ç¨)ã ä¾ãã°ã post
ãªãã¸ã§ã¯ãã®å ´å:
post: {
id: 1,
title: 'My Journey with Vue'
}
次ã®ãã³ãã¬ã¼ãã¯:
<blog-post v-bind="post"></blog-post>
以ä¸ã¨åçã¨ãªãã¾ã:
<blog-post
v-bind:id="post.id"
v-bind:title="post.title"
></blog-post>
åæ¹åã®ãã¼ã¿ããã¼
å ¨ã¦ã®ããããã£ã¯ãåããããã£ã¨è¦ªããããã£ã®éã« åæ¹åã®ãã¤ã³ãã£ã³ã° ãå½¢æãã¾ã: 親ã®ããããã£ãæ´æ°ãããã¨åã¸ã¨æµãè½ã¡ã¦ããã¾ãããéåãã«ãã¼ã¿ãæµãããã¨ã¯ããã¾ãããããã«ãã£ã¦ãåã³ã³ãã¼ãã³ãã誤ã£ã¦è¦ªã®ç¶æ ãå¤æ´ãããã¨(ã¢ããªã®ãã¼ã¿ããã¼ãç解ãã¥ãããããã¨ãããã¾ã)ãé²ãã¾ãã
ã¾ãã親ã³ã³ãã¼ãã³ããæ´æ°ããããã³ã«ãåã³ã³ãã¼ãã³ãå ã®å ¨ã¦ã®ããããã£ãææ°ã®å¤ã¸ã¨æ´æ°ããã¾ããããã¯ãåã³ã³ãã¼ãã³ãå ã§ããããã£ã®å¤ãå¤åããã¦ã¯ ãããªã ãã¨ãæå³ãã¦ãã¾ãããããè¡ã£ãå ´åã Vue 㯠ã³ã³ã½ã¼ã«ã«ã¦è¦åãã¾ãã
ããããã£ã®å¤ãå¤åããããå ´åã¯ä¸»ã« 2 ã¤ããã¾ã:
ããããã£ãåæå¤ã¨ãã¦åã渡ããåã³ã³ãã¼ãã³ãã«ã¦ãã¼ã«ã«ã®ãã¼ã¿ã¨ãã¦å¾ã§å©ç¨ãããã¨èããå ´åã ãã®å ´åãããããã£ã®å¤ããã¼ã«ã«ã® data ã®åæå¤ã¨ãã¦å®ç¾©ãããã¨ãæ¨å¥¨ãã¾ã:
props: ['initialCounter'], data: function () { return { counter: this.initialCounter } }
ããããã£ãæªå å·¥ã®å¤ã¨ãã¦æ¸¡ãå ´åã ãã®å ´åãããããã£ã®å¤ã使ç¨ããç®åºããããã£ãå¥éå®ç¾©ãããã¨ãæ¨å¥¨ãã¾ã:
props: ['size'], computed: { normalizedSize: function () { return this.size.trim().toLowerCase() } }
JavaScript ã®ãªãã¸ã§ã¯ãã¨é åã¯ãåç §æ¸¡ãããããã¨ã«æ³¨æãã¦ãã ãããåç §ã¨ãã¦æ¸¡ããããããåã³ã³ãã¼ãã³ãå ã§é åããªãã¸ã§ã¯ããå¤æ´ããã¨ã 親ã®ç¶æ ã¸ã¨å½±é¿ãã¾ãã
ããããã£ã®ããªãã¼ã·ã§ã³
ã³ã³ãã¼ãã³ãã¯ããããã£ã«å¯¾ãã¦ãä¸ã§è¦ãããã«åãªã©ã®è¦ä»¶ãæå®ãããã¨ãã§ãã¾ããããæå®ããè¦ä»¶ãæºããããªãå ´åã Vue ã¯ãã©ã¦ã¶ã® JavaScript ã³ã³ã½ã¼ã«ã«ã¦è¦åãã¾ããããã¯ãä»ã®äººãå©ç¨ãããã¨ãæå³ããã³ã³ãã¼ãã³ããéçºããå ´åã«ãç¹ã«ä¾¿å©ã§ãã
ããããã£ã®ããªãã¼ã·ã§ã³ã¯ãæååã®é
åã®ä»£ããã«ã props
ã®å¤ã«ã¤ãã¦ã®ããªãã¼ã·ã§ã³è¦ä»¶ããã£ããªãã¸ã§ã¯ãã渡ããã¨ã§æå®ã§ãã¾ãã ä¾ãã°ä»¥ä¸ã®ãããªãã®ã§ã:
Vue.component('my-component', {
props: {
// åºæ¬çãªåã®æ¤æ» (`null` 㨠`undefined` ã¯å
¨ã¦ã®ããªãã¼ã·ã§ã³ã«ãã¹ãã¾ã)
propA: Number,
// è¤æ°ã®åã®è¨±å®¹
propB: [String, Number],
// æåååãå¿
é ã§è¦æ±ãã
propC: {
type: String,
required: true
},
// ããã©ã«ãå¤ã¤ãã®æ°å¤å
propD: {
type: Number,
default: 100
},
// ããã©ã«ãå¤ã¤ãã®ãªãã¸ã§ã¯ãå
propE: {
type: Object,
// ãªãã¸ã§ã¯ããããã¯é
åã®ããã©ã«ãå¤ã¯
// å¿
ããããçã¿åºãããã®é¢æ°ãè¿ãå¿
è¦ãããã¾ãã
default: function () {
return { message: 'hello' }
}
},
// ã«ã¹ã¿ãã¤ãºããããªãã¼ã·ã§ã³é¢æ°
propF: {
validator: function (value) {
// ããããã£ã®å¤ã¯ãå¿
ãããããã®æååã§ãªããã°ãªããªã
return ['success', 'warning', 'danger'].indexOf(value) !== -1
}
}
}
})
ããããã£ã®ããªãã¼ã·ã§ã³ã失æããå ´åã Vue ã¯ã³ã³ã½ã¼ã«ã«ã¦è¦åãã¾ã (éçºç¨ãã«ããå©ç¨ãã¦ããã¨ã)ã
ããããã£ã®ããªãã¼ã·ã§ã³ã¯ã³ã³ãã¼ãã³ãã®ã¤ã³ã¹ã¿ã³ã¹ãçæããã å ã«è¡ããããããã¤ã³ã¹ã¿ã³ã¹ã®ãããã㣠(ä¾ãã° data
, computed
ãªã©) ã dafault
ããã³ validator
é¢æ°ã®ä¸ã§å©ç¨ãããã¨ã¯ã§ãã¾ããã
åã®æ¤æ»
type
ã¯ã次ã®ãã¤ãã£ãã³ã³ã¹ãã©ã¯ã¿ã®ããããã§ã:
- String
- Number
- Boolean
- Array
- Object
- Date
- Function
- Symbol
ããã«ã type
ã«ã¯ã«ã¹ã¿ã ã³ã³ã¹ãã©ã¯ã¿é¢æ°ãã¨ããã¨ãã§ãã instanceof
ã«ããã¢ãµã¼ã·ã§ã³ãè¡ããã¾ããä¾ãã°ã以ä¸ã®ã³ã³ã¹ãã©ã¯ã¿é¢æ°ãåå¨ããã¨ä»®å®ããã¨ã:
function Person (firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
ãã®ããã«å©ç¨ãããã¨ãã§ãã¾ã:
Vue.component('blog-post', {
props: {
author: Person
}
})
ä¸ã®ä¾ã§ã¯ã author
ããããã£ã®å¤ã new Person
ã«ãã£ã¦ä½æããããã®ãã©ããæ¤è¨¼ããã¾ãã
ããããã£ã§ãªãå±æ§
ããããã£ã§ãªãå±æ§ã¨ã¯ãã³ã³ãã¼ãã³ãã«æ¸¡ãããå±æ§ã®ãã¡ã対å¿ããããããã£ãå®ç¾©ããã¦ããªããã®ã®ãã¨ã§ãã
åã³ã³ãã¼ãã³ãã¸æ å ±ãåã渡ãã¨ããç¨éãèããã¨ãããããã£ã¯æ示çã«å®ç¾©ããããã¨ã好ã¾ããã§ãããã³ã³ãã¼ãã³ãã©ã¤ãã©ãªã®ä½æè ã¯ãã³ã³ãã¼ãã³ãã使ç¨ããããã³ã³ããã¹ããå¿ ãããææ¡ã§ããããã§ã¯ããã¾ããããã®ãããã³ã³ãã¼ãã³ãã¯ä»»æã®å±æ§ãåãå ¥ããããããã«ãªã£ã¦ãã¾ããåãå ¥ããå±æ§ã¯ã³ã³ãã¼ãã³ãã®ã«ã¼ãè¦ç´ ã«è¿½å ããã¾ãã
ä¾ãã°ããµã¼ããã¼ãã£ã® bootstrap-date-input
ã³ã³ãã¼ãã³ãã¨ã input
ã« data-date-picker
å±æ§ãè¦æ±ãã Bootstrap ãã©ã°ã¤ã³ã使ã£ã¦ããã¨ãã¾ãããã®å ´åããã®å±æ§ãã³ã³ãã¼ãã³ãã®ã¤ã³ã¹ã¿ã³ã¹ã«è¿½å ã§ãã¾ã:
<bootstrap-date-input data-date-picker="activated"></bootstrap-date-input>
ããã¨ãdata-date-picker="activated"
å±æ§ã¯èªåçã« bootstrap-date-input
ã®ã«ã¼ãè¦ç´ ã«è¿½å ããã¾ãã
æ¢åã®å±æ§ã¸ã®ç½®æã¨ãã¼ã¸
ããã bootstrap-date-input
å´ã®ãã³ãã¬ã¼ãã¨ãã¾ã:
<input type="date" class="form-control">
ãã®æ¥ä»ãã©ã°ã¤ã³ã®ãã¼ããæå®ããã«ã¯ã以ä¸ã®ãããªç¹å®ã®ã¯ã©ã¹ã追å ããå¿ è¦ãããã¾ã:
<bootstrap-date-input
data-date-picker="activated"
class="date-picker-theme-dark"
></bootstrap-date-input>
ä¸ã®ä¾ã§ã¯ãã¯ã©ã¹ã«2ã¤ã®ç°ãªãå¤ãå®ç¾©ããã¦ãã¾ã:
form-control
ã¯ã³ã³ãã¼ãã³ãã®ãã³ãã¬ã¼ãå ã§å®ç¾©ããã¦ãã¾ãdate-picker-theme-dark
ã¯ãã³ã³ãã¼ãã³ãã®è¦ªããåã渡ããã¾ã
ã»ã¨ãã©ã®å±æ§ã«ããã¦ã¯ãã³ã³ãã¼ãã³ãå´ã®å¤ããã³ã³ãã¼ãã³ãã«åã渡ãããå¤ã¸ã¨ç½®æããã¾ãããã¨ãã°ã type="text"
ã渡ãã¨ã type="date"
ã¯ç½®ãæããããããã¦å£ãã¦ãã¾ãã§ãããï¼å¹¸éãªãã¨ã«ã class
ããã³ style
å±æ§ã¯å°ãã¹ãã¼ãã«ä½ããã¦ãã¾ãã®ã§ã両æ¹ã®å¤ããã¼ã¸ãããæçµçãªå¤ã¯ form-control date-picker-theme-dark
ã¨ãªãã¾ãã
å±æ§ã®ç¶æ¿ã®ç¡å¹å
ã³ã³ãã¼ãã³ãã®ã«ã¼ãè¦ç´ ã«å¯¾ãã¦ãå±æ§ãç¶æ¿ãããã ãªã å ´åã¯ãã³ã³ãã¼ãã³ãã®ãªãã·ã§ã³å
ã«ã¦ inheritAttrs: false
ãè¨å®ã§ãã¾ããä¾:
Vue.component('my-component', {
inheritAttrs: false,
// ...
})
ããã¯ã ã³ã³ãã¼ãã³ãã® $attrs
ã¤ã³ã¹ã¿ã³ã¹ããããã£ã¨çµã¿åãããæã«ç¹ã«ä¾¿å©ã§ãã $attrs
ã¯ãã³ã³ãã¼ãã³ãã«æ¸¡ãããå±æ§åããã³ãã®å¤ãå«ã¾ãã¦ãã¾ã :
{
required: true,
placeholder: 'Enter your username'
}
inheritAttrs: false
㨠$attrs
ãä½µç¨ããã¨ãã©ã®è¦ç´ ã«å±æ§ãéãããæåã§æ±ºå®ãããã¨ãã§ãã¾ããããã¯å¤ãã®å ´åã åºåºã³ã³ãã¼ãã³ã ã«ã¨ã£ã¦æã¾ããæ¹æ³ã§ã:
Vue.component('base-input', {
inheritAttrs: false,
props: ['label', 'value'],
template: `
<label>
{{ label }}
<input
v-bind="$attrs"
v-bind:value="value"
v-on:input="$emit('input', $event.target.value)"
>
</label>
`
})
inheritAttrs: false
ãªãã·ã§ã³ã¯ãstyle
ããã³ class
å±æ§ã®ãã¤ã³ãã£ã³ã°ã«ã¯å½±é¿ ããªã ãã¨ã«æ³¨æãã¦ãã ããã
ãã®ãã¿ã¼ã³ã使ç¨ããã¨ãã³ã³ãã¼ãã³ãã®ã«ã¼ãè¦ç´ ãä½ãæ°ã«ãããã¨ãªããçã® HTML è¦ç´ ã®ããã«åºåºã³ã³ãã¼ãã³ããå©ç¨ã§ãã¾ãã
<base-input
label="Username:"
v-model="username"
required
placeholder="Enter your username"
></base-input>