ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- 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å¹´9æ4æ¥
Vue.js ã§ã¯ HTML ãã¼ã¹ã®ãã³ãã¬ã¼ãæ§æã使ã£ã¦ããã®ã§ãVue ã¤ã³ã¹ã¿ã³ã¹ã®ãã¼ã¿ã¨æç»ããã DOM ã宣è¨çã«å¯¾å¿ããããã¨ãã§ãã¾ããå ¨ã¦ã® Vue.js ãã³ãã¬ã¼ãã¯ãä»æ§ã«æºæ ãã¦ãããã©ã¦ã¶ã HTML ãã¼ãµã«ãã£ã¦ãã¼ã¹ã§ããæå¹ãª HTML ã§ãã
å é¨ã§ã¯ãVue ã¯ãã³ãã¬ã¼ãã Virtual DOM ã®æç»é¢æ°ã«ã³ã³ãã¤ã«ãã¾ãããªã¢ã¯ãã£ãã·ã¹ãã ã¨çµã¿åããã¦ãVue ã¯åæç»ã«å¿ è¦ãªã³ã³ãã¼ãã³ããã¤ã³ããªã¸ã§ã³ãã«ææ¡ã§ããã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãå¤ãã£ãæã«æä½éã® DOM æä½ãé©ç¨ãã¾ãã
ãããããªãã Virtual DOM ã®æ¦è¦ã«è©³ãããJavaScript ã§ç´æ¥æç»ããã®ã好ãå ´åããã³ãã¬ã¼ãã®ä»£ããã«ç´æ¥ render é¢æ°ã§æ¸ã ãã¨ãå¯è½ã§ããªãã·ã§ã³ã§ JSX ããµãã¼ããã¦ãã¾ãã
å±é
ããã¹ã
ãã¼ã¿ãã¤ã³ãã£ã³ã°ã®ãã£ã¨ãåºæ¬çãªå½¢ã¯ãâMustacheâ æ§æ(äºéä¸æ¬å¼§)ãå©ç¨ããããã¹ãå±éã§ã:
<span>Message: {{ msg }}</span>
mustache ã¿ã°ã¯ã対å¿ãããªãã¸ã§ã¯ãã® msg
ããããã£ã®å¤ã«ç½®ãæãããã¾ããã¾ããmsg
ããããã£ãå¤æ´ãããæãããã«å¿ãã¦æ´æ°ããã¾ãã
v-once ãã£ã¬ã¯ãã£ãã使ç¨ãããã¨ã§ããã¼ã¿å¤æ´æã®æ´æ°ã¯ãããªãããä¸åº¦ã ãå±éãããã¨ãã§ãã¾ãããã ããåããã¼ãã®ããããä»ã®ãã¤ã³ãã£ã³ã°ãå½±é¿ãåãããã¨ã«æ³¨æãã¦ãã ãã:
<span v-once>This will never change: {{ msg }}</span>
çã® HTML
2éä¸æ¬å¼§ã® mustaches ã¯ããã¼ã¿ã HTML ã§ã¯ãªãããã¬ã¼ã³ãªããã¹ãã¨ãã¦æ±ãã¾ããå®éã® HTML ã¨ãã¦åºåããããã«ã¯ãv-html
ãã£ã¬ã¯ãã£ãã使ç¨ããå¿
è¦ãããã¾ã:
<p>Using mustaches: {{ rawHtml }}</p>
<p>Using v-html directive: <span v-html="rawHtml"></span></p>
Using mustaches: {{ rawHtml }}
Using v-html directive:
ãã® span
ã®ã³ã³ãã³ã㯠rawHtml
ããããã£ã®å¤ã«ç½®ãæãããããã¬ã¼ã³ãª HTML ã¨ãã¦è§£éããã¾ããVue ã¯ãæååãã¼ã¹ã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã§ã¯ãªãã®ã§ãv-html
ããã³ãã¬ã¼ãé¨åãæ§æãã¦ä½¿ç¨ã§ããªããã¨ã«æ³¨æãã¾ãããã代ããã«ã UI ã®åå©ç¨ãçµã¿åããã®ããã®åºç¤ã¨ãã¦ãã³ã³ãã¼ãã³ããå©ç¨ãããã¨ã好ã¾ããã§ãã
XSS èå¼±æ§ã容æã«å¼ãèµ·ããã®ã§ãã¦ã§ããµã¤ãã§åçã«ä»»æã®HTMLãæç»ãããã¨ã¯ãé常ã«å±éºã§ããä¿¡é ¼ã§ããã³ã³ãã³ãã«ã ã HTML å±éãå©ç¨ãã¦ãã ãããã¦ã¼ã¶ã¼ããæä¾ãããã³ã³ãã³ãã«å¯¾ãã¦ã¯æ±ºãã¦ä½¿ç¨ãã¦ã¯ããã¾ããã
å±æ§
Mustache ã¯ãHTML å±æ§ã®å
é¨ã§ä½¿ç¨ãããã¨ã¯ã§ãã¾ããã代ããã«ãv-bind
ãã£ã¬ã¯ãã£ãã使ç¨ãã¦ãã ãã:
<div v-bind:id="dynamicId"></div>
å±æ§ãåã«åå¨ãã¦ãããã¨ã true
ã¨ç¤ºãã¨ãã£ãçå½å¤å±æ§ã®å ´åãv-bind
ã¯å°ãç°ãªã£ãåãããã¾ãããã®ä¾ã§ã¯ï¼
<button v-bind:disabled="isButtonDisabled">Button</button>
isButtonDisabled
ã null
ãundefined
ãã¾ã㯠false
ã®å¤ãæã¤å ´åãdisabled
å±æ§ã¯æç»ããã <button>
è¦ç´ ã«å«ãããã¾ããã
JavaScript å¼ã®ä½¿ç¨
ããã¾ã§ããã³ãã¬ã¼ãã«åç´ãªãã¼ããã¤ã³ãã£ã³ã°ãã¦ãã¾ãããå®éã«ã¯ Vue.js ã¯å ¨ã¦ã®ãã¼ã¿ãã¤ã³ãã£ã³ã°å é¨ã§ JavaScript å¼ãå®å ¨ã«ãµãã¼ããã¾ã:
{{ number + 1 }}
{{ ok ? 'YES' : 'NO' }}
{{ message.split('').reverse().join('') }}
<div v-bind:id="'list-' + id"></div>
ãããã®å¼ã¯ãVue ã¤ã³ã¹ã¿ã³ã¹ãææãããã¼ã¿ã¹ã³ã¼ãå 㧠JavaScript ã¨ãã¦è©ä¾¡ããã¾ããå¶éã¨ãã¦ãããããã®ãã¤ã³ãã£ã³ã°ã¯ãåä¸ã®å¼ã ãå«ããã¨ãã§ããã¨ãããã®ã§ãããªã®ã§ã以ä¸ã¯åä½ãã¾ãã:
<!-- ããã¯æã§ãããå¼ã§ã¯ããã¾ãã: -->
{{ var a = 1 }}
<!-- ããã¼å¶å¾¡ãããããåä½ãã¾ãããä¸é
æ¼ç®åã使ç¨ãã¦ãã ãã -->
{{ if (ok) { return message } }}
ãã³ãã¬ã¼ãå¼ã¯ãµã³ãããã¯ã¹ã§ãMath
ã Date
ã¨ãã£ã ãã¯ã¤ããªã¹ãã«ããã°ãã¼ãã«ãªãã¸ã§ã¯ã ã ãã«ã¢ã¯ã»ã¹ã§ãã¾ãããã³ãã¬ã¼ãå¼å
ã§ã¦ã¼ã¶ã¼ãå®ç¾©ããã°ãã¼ãã«ãªãã¸ã§ã¯ãã«ã¢ã¯ã»ã¹ãããã¨ãã¦ã¯ããã¾ããã
ãã£ã¬ã¯ãã£ã
ãã£ã¬ã¯ãã£ã㯠v-
ããå§ã¾ãç¹å¥ãªå±æ§ã§ãããã£ã¬ã¯ãã£ãå±æ§å¤ã¯ãåä¸ã® JavaScript å¼ãæå¾
ãã¾ã(ãã ããv-for
ã¯ä¾å¤ã§ãããã«ã¤ãã¦ã¯å¾ãã説æãã¾ã)ããã£ã¬ã¯ãã£ãã®ä»äºã¯ãå±æ§å¤ã®å¼ãå¤åããã¨ãã«ããªã¢ã¯ãã£ãã«å¯ä½ç¨ã DOM ã«é©ç¨ãããã¨ã§ããã¤ã³ãããã¯ã·ã§ã³ã§è¦ãä¾ãæ¯ãè¿ã£ã¦ã¿ã¾ããã:
<p v-if="seen">Now you see me</p>
ããã§ã® v-if
ãã£ã¬ã¯ãã£ã㯠seen
å¼ã®å¤ãçãå¦ãã«åºã¥ãã¦ã <p>
è¦ç´ ãåé¤/æ¿å
¥ãã¾ãã
å¼æ°
ãã£ã¬ã¯ãã£ãã®ä¸ã«ã¯ âå¼æ°â ãåããã®ãããã¾ããããã¯ãã£ã¬ã¯ãã£ãåã®å¾ã«ã³ãã³ã§è¡¨è¨ãã¾ããä¾ãã°ãv-bind
ãã£ã¬ã¯ãã£ãã¯ããªã¢ã¯ãã£ãã« HTML å±æ§ãæ´æ°ãã¾ã:
<a v-bind:href="url"> ... </a>
ããã§ã® href
㯠v-bind
ãã£ã¬ã¯ãã£ãã«è¦ç´ ã® href
å±æ§ã«å¼ url
ã®å¤ãæç¸ãããã¨ãæããããã®å¼æ°ã§ãã
v-on
ãã£ã¬ã¯ãã£ãã®å¥ã®ä¾ãè¦ã¦ã¿ã¾ãããããã㯠DOM ã¤ãã³ããåãåãã¾ã:
<a v-on:click="doSomething"> ... </a>
ããã§ã®å¼æ°ã¯åãåãããã¤ãã³ãåã§ããããããã¤ãã³ããã³ããªã³ã°ã®è©³ç´°ã«ã¤ãã¦èª¬æãã¾ãã
åçå¼æ°
2.6.0 ããæ°è¦
ãã¼ã¸ã§ã³ 2.6.0 ãããè§æ¬å¼§ã§å²ããã¨ã§ JavaScript å¼ããã£ã¬ã¯ãã£ãã®å¼æ°ã«ä½¿ããã¨ãã§ãã¾ã:
<!--
åçå¼æ°ã«ã¯ã"åçå¼æ°ã®å¼ã®å¶ç´" ã®ç¯ã§å¾è¿°ãããããã«ãããã¤ãã®å¶ç´ãããç¹ã«æ³¨æãã¦ãã ããã
-->
<a v-bind:[attributeName]="url"> ... </a>
ãã㧠attributeName
㯠JavaScript å¼ã¨ãã¦åçã«è©ä¾¡ããããã®è©ä¾¡çµæãå¼æ°ã®æçµçãªå¤ã¨ãã¦ä½¿ããã¾ããä¾ãã°ãVue ã¤ã³ã¹ã¿ã³ã¹ã "href"
ã¨ããå¤ã® attributeName
ã¨ãã data ããããã£ããã¤å ´åããã®ãã¤ã³ãã£ã³ã°ã¯ v-bind:href
ã¨çãããªãã¾ãã
åæ§ã«ãåçãªã¤ãã³ãåã«ãã³ãã©ããã¤ã³ãããããã«åçå¼æ°ã使ããã¨ãã§ãã¾ã:
<a v-on:[eventName]="doSomething"> ... </a>
ãã®ä¾ã§ã¯ãeventName
ã®å¤ã "focus"
ã ã¨ããã¨ãv-on:[eventName]
㯠v-on:focus
ã¨çãããªãã¾ãã
åçå¼æ°ã®å¤ã®å¶ç´
åçå¼æ°ã¯ãnull
ãé¤ã㨠string ã«è©ä¾¡ããããã¨ãæ³å®ããã¦ãã¾ããç¹æ®å¤ null
ã¯ãæ示çã«ãã¤ã³ãã£ã³ã°ãåé¤ããã®ã«ä½¿ããã¾ãããã®ä»ã® string 以å¤ã®å¤ã¯ãè¦åãå¼ãèµ·ããã¾ãã
åçå¼æ°ã®å¼ã®å¶ç´
åçå¼æ°ã®å¼ã«ã¯æ§æä¸ã®å¶ç´ãããã¾ããã¨ããã®ããã¹ãã¼ã¹ãå¼ç¨ç¬¦ã®ãããªä¸é¨ã®æåã¯ãHTML ã®å±æ§åã¨ãã¦ã¯ä¸æ£ãªæåã ããã§ããä¾ãã°ã以ä¸ã¯ä¸æ£ã§ã:
<!-- ããã¯ã³ã³ãã¤ã©è¦åãå¼ãèµ·ããã¾ã -->
<a v-bind:['foo' + bar]="value"> ... </a>
åé¿çã¯ãã¹ãã¼ã¹ãå¼ç¨ç¬¦ãå«ã¾ãªãå¼ã使ãããè¤éãªå¼ãç®åºããããã£ã§ç½®ãæãããã§ãã
in-DOM ãã³ãã¬ã¼ã (ããªãã¡ãHTML ãã¡ã¤ã«ã«ç´æ¥æ¸ããããã³ãã¬ã¼ã) ã使ãå ´åããã©ã¦ã¶ãå¼·å¶çã«å±æ§åãå°æåã«ããããããã¼åã大æåã«ããã®ã¯é¿ããã¹ãã§ã:
<!--
in-DOM ãã³ãã¬ã¼ãã®ä¸ã§ã¯ãv-bind:[someattr] ã«å¤æããã¾ãã
ã¤ã³ã¹ã¿ã³ã¹ã« "someattr" ããããã£ããªãå ´åããã®ã³ã¼ãã¯åä½ãã¾ããã
-->
<a v-bind:[someAttr]="value"> ... </a>
修飾å
修飾å (Modifier) ã¯ããããã§è¡¨è¨ãããç¹å¥ãªæ¥å°¾èªã§ããã£ã¬ã¯ãã£ããç¹å¥ãªæ¹æ³ã§æç¸ãããã¹ãã¨ãããã¨ã示ãã¾ããä¾ãã°ã.prevent
修飾å㯠v-on
ãã£ã¬ã¯ãã£ãã«ãã¤ãã³ããããªã¬ãããé event.preventDefault()
ãå¼ã¶ããã«ä¼ãã¾ã:
<form v-on:submit.prevent="onSubmit"> ... </form>
å¾ã»ã©v-on
ããã³v-model
ã®èª¬æãããéã修飾åã®ä»ã®ä¾ãè¦ãã§ãããã
çç¥è¨æ³
v-
æ¥é è¾ã¯ããã³ãã¬ã¼ãå
ã® Vue ç¬èªã®å±æ§ãèå¥ããããã®ç®å°ã¨ãªã£ã¦ãã¾ããããã¯æ¢åã®ãã¼ã¯ã¢ããã«å¯¾ãã¦ã Vue.js ãå©ç¨ãã¦åçãªæ¯ãèããé©ç¨ããå ´åã«ä¾¿å©ã§ãããé »ç¹ã«å©ç¨ããããã£ã¬ã¯ãã£ãã«å¯¾ãã¦ã¯åé·ã«æãããã¨ãããã§ããããåæã«ã·ã³ã°ã«ãã¼ã¸ã¢ããªã±ã¼ã·ã§ã³ãä½æããã«ããããå
¨ã¦ã®ãã³ãã¬ã¼ãã Vue ã§ç®¡çãã¦ããã¨ããv-
æ¥é è¾ãä»ããå¿
è¦æ§ã¯ä½ããã®ã«ãªãã§ãããããããã£ã¦ã Vue 㯠2 ã¤ã®æããã使ããããã£ã¬ã¯ãã£ã v-bind
㨠v-on
ã«å¯¾ãã¦ç¹å¥ãªçç¥è¨æ³ãæä¾ãã¦ãã¾ã:
v-bind
çç¥è¨æ³
<!-- å®å
¨ãªæ§æ -->
<a v-bind:href="url"> ... </a>
<!-- çç¥è¨æ³ -->
<a :href="url"> ... </a>
<!-- åçå¼æ°ã®çç¥è¨æ³ (2.6.0 以é) -->
<a :[key]="url"> ... </a>
v-on
çç¥è¨æ³
<!-- å®å
¨ãªæ§æ -->
<a v-on:click="doSomething"> ... </a>
<!-- çç¥è¨æ³ -->
<a @click="doSomething"> ... </a>
<!-- åçå¼æ°ã®çç¥è¨æ³ (2.6.0 以é) -->
<a @[event]="doSomething"> ... </a>
ãããã¯æ®éã® HTML ã¨ã¯ã¡ãã£ã¨éãããã«è¦ããããããã¾ãããã§ããã :
ã @
ã¯å±æ§åã«å©ç¨å¯è½ãªæåã§ããVue ã®ãµãã¼ããã¦ãããã¹ã¦ã®ãã©ã¦ã¶ã§ãæ£ãããã¼ã¹ãããã¨ãã§ãã¾ããå ãã¦ãæçµçã«æç»ããããã¼ã¯ã¢ããã«ãããã¯ç¾ãã¾ãããçç¥è¨æ³ã®æ§æã®å©ç¨ã¯å®å
¨ã«ä»»æã§ãããå¾ã§ãã®ä½¿ç¨æ¹æ³ã«ã¤ãã¦è©³ããå¦ãã æã«ä¾¿å©ã¨æãããã¨ã§ãããã