ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- 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å¹´2æ20æ¥
ãã®ãã¼ã¸ã¯ ã³ã³ãã¼ãã³ãã®åºæ¬ ãèªã¾ãã¦ãããã¨ãåæã«ãªã£ã¦ãã¾ããã³ã³ãã¼ãã³ããæ±ã£ãäºã®ãªãå ´åã¯ãã¡ãã®ãã¼ã¸ãå ã«èªãã§ãã ããã
ãã¼ã¸ã§ã³ 2.6.0 ã§ãååä»ãã¹ãããã¨ã¹ã³ã¼ãä»ãã¹ãããã«å¯¾ããæ°ããçµ±ä¸æ§æ (
v-slot
ãã£ã¬ã¯ãã£ã) ãå°å ¥ããã¾ãããslot
ããã³slot-scope
å±æ§ã¯éæ¨å¥¨ã¨ãªãç½®ãæãããã¾ãããã¾ã åé¤ã¯ ããã ãããã¥ã¡ã³ãã ãã ã«ããã¾ããæ°ããæ§æãå°å ¥ããçç±ã¯ããã® RFC ã«è¨è¿°ããã¦ãã¾ãã
ã¹ãããã³ã³ãã³ã
Vue ã«ã¯ Web Components spec draft ã«ãã³ããå¾ãã³ã³ãã³ãé
ä¿¡ API ãå®è£
ããã¦ããã <slot>
è¦ç´ ãã³ã³ãã³ãé
ä¿¡ã®åã渡ãå£ã¨ãã¦å©ç¨ãã¾ãã
ããã使ããã¨ã§æ¬¡ã®ãããªã³ã³ãã¼ãã³ããä½æãããã¨ãåºæ¥ã¾ã:
<navigation-link url="/profile">
Your Profile
</navigation-link>
ããã¦ã <navigation-link>
ã®ãã³ãã¬ã¼ãã¯ãããªãã¾ã:
<a
v-bind:href="url"
class="nav-link"
>
<slot></slot>
</a>
ã³ã³ãã¼ãã³ããæç»ããæã <slot></slot>
ã¯ãYour Profileãã«ç½®æãããã§ããããã¹ãããã«ã¯ HTML ãå«ãä»»æã®ãã³ãã¬ã¼ããå
¥ãããã¨ãåºæ¥ã¾ã:
<navigation-link url="/profile">
<!-- Font Awesome ã®ã¢ã¤ã³ã³ã追å -->
<span class="fa fa-user"></span>
Your Profile
</navigation-link>
ã¾ãã¯ãä»ã®ã³ã³ãã¼ãã³ããå ¥ãããã¨ãåºæ¥ã¾ã:
<navigation-link url="/profile">
<!-- ã³ã³ãã¼ãã³ãã使ã£ã¦ã¢ã¤ã³ã³ã追å -->
<font-awesome-icon name="user"></font-awesome-icon>
Your Profile
</navigation-link>
ããã <navigation-link>
ã®ãã³ãã¬ã¼ãã <slot>
è¦ç´ ãå«ã¾ ãªã å ´åãéå§ã¿ã°ã¨çµäºã¿ã°ã®éã«ããä»»æã®ã³ã³ãã³ãã¯ç ´æ£ããã¾ãã
ã³ã³ãã¤ã«ã¹ã³ã¼ã
ã¹ãããã®ä¸ã§ãã¼ã¿ãæ±ãããå ´åã¯ãããã¾ã:
<navigation-link url="/profile">
Logged in as {{ user.name }}
</navigation-link>
ãã®ã¹ãããã¯ããã³ãã¬ã¼ãã®æ®ãã®é¨åã¨åãã¤ã³ã¹ã¿ã³ã¹ãããã㣠(ã¤ã¾ããåã âã¹ã³ã¼ãâ) ã«ã¢ã¯ã»ã¹ã§ãã¾ãã<navigation-link>
ã®ã¹ã³ã¼ãã«ã¢ã¯ã»ã¹ãããã¨ã¯ ã§ãã¾ãããä¾ãã°ãurl
ã¸ã®ã¢ã¯ã»ã¹ã¯åä½ããªãã§ããã:
<navigation-link url="/profile">
Clicking here will send you to: {{ url }}
<!--
`url` 㯠undefined ã«ãªãã¾ããã¨ããã®ãããã®ã³ã³ãã³ãã¯
<navigation-link> ã³ã³ãã¼ãã³ã _ã®ä¸ã§_ å®ç¾©ãããã®ã§ã¯ãªãã
<navigation-link> ã³ã³ãã¼ãã³ã _ã«_ 渡ãããããã§ãã
-->
</navigation-link>
ã«ã¼ã«ã¨ãã¦ã¯ã以ä¸ãè¦ãã¦ããã¦ãã ãã:
親ãã³ãã¬ã¼ãå ã®å ¨ã¦ã®ãã®ã¯è¦ªã®ã¹ã³ã¼ãã§ã³ã³ãã¤ã«ãããåãã³ãã¬ã¼ãå ã®å ¨ã¦ã®ãã®ã¯åã®ã¹ã³ã¼ãã§ã³ã³ãã¤ã«ãããã
ãã©ã¼ã«ããã¯ã³ã³ãã³ã
ã¹ãããã«å¯¾ãã¦ãã³ã³ãã³ãããªãå ´åã«ã ãæç»ããããã©ã¼ã«ãã㯠(ã¤ã¾ããããã©ã«ãã®) ã³ã³ãã³ããæå®ããã¨ä¾¿å©ãªå ´åãããã¾ããä¾ãã°ã<submit-button>
ã³ã³ãã¼ãã³ãã«ããã¦:
<button type="submit">
<slot></slot>
</button>
ã»ã¨ãã©ã®å ´åã«ã¯ <button>
ã®ä¸ã«ãSubmitãã¨ããæåãæç»ãããããããã¾ããããSubmitãããã©ã¼ã«ããã¯ã³ã³ãã³ãã«ããã«ã¯ã<slot>
ã¿ã°ã®ä¸ã«è¨è¿°ãã¾ãã
<button type="submit">
<slot>Submit</slot>
</button>
ããã¦ã親ã³ã³ãã¼ãã³ãããã¹ãããã®ã³ã³ãã³ããæå®ããã« <submit-button>
ã使ãã¨:
<submit-button></submit-button>
ãã©ã¼ã«ããã¯ã³ã³ãã³ãã®ãSubmitããæç»ããã¾ã:
<button type="submit">
Submit
</button>
ããããããã³ã³ãã³ããæå®ããã¨:
<submit-button>
Save
</submit-button>
æå®ãããã³ã³ãã³ãã代ããã«æç»ããã¾ã:
<button type="submit">
Save
</button>
ååä»ãã¹ããã
2.6.0 ããæ´æ°ã
slot
å±æ§ã使ã£ãéæ¨å¥¨ã®æ§æã«ã¤ãã¦ã¯ããã¡ããåç §
è¤æ°ã®ã¹ããããããã¨ä¾¿å©ãªã¨ããããã¾ããä¾ãã°ã<base-layout>
ã³ã³ãã¼ãã³ããä¸è¨ã®ãããªãã³ãã¬ã¼ãã ã¨ãã¾ããã:
<div class="container">
<header>
<!-- ããã«ãããã³ã³ãã³ã -->
</header>
<main>
<!-- ããã«ã¡ã¤ã³ã³ã³ãã³ã -->
</main>
<footer>
<!-- ããã«ããã¿ã¼ã³ã³ãã³ã -->
</footer>
</div>
ãããã£ãå ´åã®ããã«ã <slot>
è¦ç´ 㯠name
ã¨ããç¹å¥ãªå±æ§ãæã£ã¦ãã¦ã追å ã®ã¹ããããå®ç¾©ã§ãã¾ã:
<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>
name
ã®ãªã <slot>
è¦ç´ ã¯ãæé»çã«ãdefaultãã¨ããååãæã¡ã¾ãã
ååä»ãã¹ãããã«ã³ã³ãã³ããæå®ããã«ã¯ã<template>
ã«å¯¾ã㦠v-slot
ãã£ã¬ã¯ãã£ãã使ã£ã¦ãã¹ãããåãå¼æ°ã¨ãã¦ä¸ãã¾ã:
<base-layout>
<template v-slot:header>
<h1>Here might be a page title</h1>
</template>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
<template v-slot:footer>
<p>Here's some contact info</p>
</template>
</base-layout>
ããã«ããã<template>
è¦ç´ ã®ä¸èº«ã¯ãã¹ã¦å¯¾å¿ããã¹ãããã«æ¸¡ããã¾ããv-slot
ã使ã£ã <template>
ã§å²ã¾ãã¦ããªãã³ã³ãã³ãã¯ãããã©ã«ãã¹ãããã«å¯¾ãããã®ã ã¨ã¿ãªããã¾ãã
ããããæ示çã«æå®ããããªããããã©ã«ãã¹ãããã®ã³ã³ãã³ãã <template>
ã§å²ããã¨ãã§ãã¾ã:
<base-layout>
<template v-slot:header>
<h1>Here might be a page title</h1>
</template>
<template v-slot:default>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
</template>
<template v-slot:footer>
<p>Here's some contact info</p>
</template>
</base-layout>
ãããã«ãããæç»ããã HTML ã¯æ¬¡ã®ããã«ãªãã¾ã:
<div class="container">
<header>
<h1>Here might be a page title</h1>
</header>
<main>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
</main>
<footer>
<p>Here's some contact info</p>
</footer>
</div>
éæ¨å¥¨ã«ãªã£ã slot
å±æ§ ã¨ã¯ç°ãªããv-slot
㯠<template>
ã ãã«è¿½å ã§ãã (1ã¤ã ãä¾å¤ ãã) ç¹ã«æ³¨æãã¦ãã ããã
ã¹ã³ã¼ãä»ãã¹ããã
2.6.0 ããæ´æ°ã
slot-scope
å±æ§ã使ã£ãéæ¨å¥¨ã®æ§æã«ã¤ãã¦ã¯ããã¡ããåç §
ã¹ãããã³ã³ãã³ããããåã³ã³ãã¼ãã³ãã®ä¸ã ãã§å©ç¨å¯è½ãªãã¼ã¿ã«ã¢ã¯ã»ã¹ã§ããã¨ä¾¿å©ãªãã¨ãããã¾ããä¾ãã°ã以ä¸ã®ãããªãã³ãã¬ã¼ãã® <current-user>
ã³ã³ãã¼ãã³ããèãã¦ã¿ã¦ãã ãã:
<span>
<slot>{{ user.lastName }}</slot>
</span>
ããã§ãã¦ã¼ã¶ã¼ã®ååã®ä»£ããã«ååã表示ããããããã®ãã©ã¼ã«ããã¯ã³ã³ãã³ããç½®ãæãããã¨æãããããã¾ãã:
<current-user>
{{ user.firstName }}
</current-user>
ããããããã¯åä½ãã¾ãããã¨ããã®ããuser
ã«ã¢ã¯ã»ã¹ãããã¨ãã§ããã®ã¯ <current-user>
ã³ã³ãã¼ãã³ãã ãã§ãããããã§æå®ãã¦ããã³ã³ãã³ãã¯è¦ªã³ã³ãã¼ãã³ãã§æç»ãããããã§ãã
親ã³ã³ãã¼ãã³ãå
ã§ã¹ãããã³ã³ãã³ãã¨ã㦠user
ã使ããããã«ããããã«ã¯ã<slot>
è¦ç´ ã®å±æ§ã¨ã㦠user
ããã¤ã³ããã¾ã:
<span>
<slot v-bind:user="user">
{{ user.lastName }}
</slot>
</span>
<slot>
è¦ç´ ã«ãã¤ã³ããããå±æ§ã¯ãã¹ããããããã㣠ã¨å¼ã°ãã¾ãã親ã¹ã³ã¼ãå
㧠v-slot
ã®å¤ã¨ãã¦ååãæå®ãããã¨ã§ãã¹ãããããããã£ãåãåããã¨ãã§ãã¾ã:
<current-user>
<template v-slot:default="slotProps">
{{ slotProps.user.firstName }}
</template>
</current-user>
ãã®ä¾ã§ã¯ããã¹ã¦ã®ã¹ãããããããã£ãä¿æãããªãã¸ã§ã¯ãã®ååã slotProps
ã«ãã¾ããããããªãã®å¥½ããªååã使ããã¨ãã§ãã¾ãã
ããã©ã«ãã¹ããããããªãå ´åã®çç¥è¨æ³
ä¸ã®ä¾ã®ããã«ããã©ã«ãã¹ããã ã ãã® å ´åã¯ãã³ã³ãã¼ãã³ãã®ã¿ã°ãã¹ãããã®ãã³ãã¬ã¼ãã¨ãã¦ä½¿ããã¨ãã§ãã¾ããã¤ã¾ããã³ã³ãã¼ãã³ãã«å¯¾ã㦠v-slot
ãç´æ¥ä½¿ãã¾ãã
<current-user v-slot:default="slotProps">
{{ slotProps.user.firstName }}
</current-user>
ããã«çããããã¨ãã§ãã¾ããæªæå®ã®ã³ã³ãã³ããããã©ã«ãã¹ãããã®ãã®ã¨ã¿ãªãããã®ã¨åæ§ã«ãå¼æ°ã®ãªã v-slot
ãããã©ã«ãã³ã³ãã³ããåç
§ãã¦ããã¨ã¿ãªããã¾ã:
<current-user v-slot="slotProps">
{{ slotProps.user.firstName }}
</current-user>
ããã©ã«ãã¹ãããã«å¯¾ããçç¥è¨æ³ã¯ãååä»ãã¹ãããã¨æ··å¨ããããã¨ã ã§ããªã ç¹ã«æ³¨æãã¦ãã ãããã¹ã³ã¼ãã®ææ§ãã«ã¤ãªããããã§ã:
<!-- ä¸æ£ãè¦åãåºã -->
<current-user v-slot="slotProps">
{{ slotProps.user.firstName }}
<template v-slot:other="otherSlotProps">
slotProps is NOT available here
</template>
</current-user>
è¤æ°ã®ã¹ããããããå ´åã¯å¸¸ã« ãã¹ã¦ã® ã¹ãããã«å¯¾ã㦠<template>
ãã¼ã¹ã®æ§æã使ç¨ãã¦ãã ãã:
<current-user>
<template v-slot:default="slotProps">
{{ slotProps.user.firstName }}
</template>
<template v-slot:other="otherSlotProps">
...
</template>
</current-user>
ã¹ãããããããã£ã®åå²ä»£å ¥
å é¨çã«ã¯ãã¹ã³ã¼ãä»ãã¹ãããã¯ã¹ãããã³ã³ãã³ããåä¸å¼æ°ã®é¢æ°ã§å²ããã¨ã§åä½ããã¦ãã¾ãã
function (slotProps) {
// ... slot content ...
}
ããã¯ãv-slot
ã®å¤ãé¢æ°å®ç¾©ã®å¼æ°é¨åã§æå¹ãªä»»æã® JavaScript å¼ãåãä»ãããã¨ãæå³ãã¾ãããã®ããããµãã¼ãããã¦ããç°å¢ (åä¸ãã¡ã¤ã«ã³ã³ãã¼ãã³ã ã¾ã㯠ã¢ãã³ãã©ã¦ã¶) ã§ã¯ãç¹å®ã®ã¹ãããããããã£ãåå¾ããããã« ES2015 ã®åå²ä»£å
¥ ã使ããã¨ãã§ãã¾ã:
<current-user v-slot="{ user }">
{{ user.firstName }}
</current-user>
ããããã¨ãã³ãã¬ã¼ãã¯ãããããã«ãªãã¾ããç¹ã«ãã¹ããããå¤ãã®ããããã£ãæä¾ãã¦ããå ´åã¯ããã§ããã¾ããããããã£ããªãã¼ã ãã (ä¾ãã°ãuser
ãã person
) ãªã©å¥ã®å¯è½æ§ãéãã¾ã:
<current-user v-slot="{ user: person }">
{{ person.firstName }}
</current-user>
ã¹ãããããããã£ãæªå®ç¾©ã ã£ãå ´åã®ãã©ã¼ã«ããã¯ãå®ç¾©ãããã¨ããã§ãã¾ã:
<current-user v-slot="{ user = { firstName: 'Guest' } }">
{{ user.firstName }}
</current-user>
åçãªã¹ãããå
2.6.0 ã§æ°è¦è¿½å
ãã£ã¬ã¯ãã£ãã®åçå¼æ° 㯠v-slot
ã§ãåä½ããåçãªã¹ãããåã®å®ç¾©ãå¯è½ã§ã:
<base-layout>
<template v-slot:[dynamicSlotName]>
...
</template>
</base-layout>
ååä»ãã¹ãããã®çç¥è¨æ³
2.6.0 ã§æ°è¦è¿½å
v-on
ã v-bind
ã¨åæ§ã« v-slot
ã«ãçç¥è¨æ³ããããå¼æ°ã®åã®ãã¹ã¦ã®é¨å (v-slot:
) ãç¹å¥ãªè¨å· #
ã§ç½®ãæãã¾ããä¾ãã°ãv-slot:header
㯠#header
ã«æ¸ãæãããã¨ãã§ãã¾ã:
<base-layout>
<template #header>
<h1>Here might be a page title</h1>
</template>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
<template #footer>
<p>Here's some contact info</p>
</template>
</base-layout>
ããããã»ãã®ãã£ã¬ã¯ãã£ãã¨åæ§ã«ãçç¥è¨æ³ã¯å¼æ°ãããå ´åã«ã®ã¿å©ç¨ã§ãã¾ããããã¯ã次ã®ãããªæ§æãä¸æ£ã¨ãããã¨ãæå³ãã¾ã:
<!-- ããã¯è¦åãå¼ãèµ·ãã -->
<current-user #="{ user }">
{{ user.firstName }}
</current-user>
代ããã«ãçç¥è¨æ³ã使ãããå ´åã«ã¯ã常ã«ã¹ãããåãæå®ããå¿ è¦ãããã¾ã:
<current-user #default="{ user }">
{{ user.firstName }}
</current-user>
ãã®ä»ã®ä¾
ã¹ãããããããã£ã使ãã°ãå ¥åããããã£ã«å¿ãã¦ç°ãªãã³ã³ãã³ããæç»ããåå©ç¨å¯è½ãªãã³ãã¬ã¼ãã«ã¹ããããå¤ãããã¨ãã§ãã¾ãã ããã¯ããã¼ã¿ãã¸ãã¯ãã«ãã»ã«åããä¸æ¹ã§è¦ªã³ã³ãã¼ãã³ãã«ããã¬ã¤ã¢ã¦ãã®ã«ã¹ã¿ãã¤ãºã許ããããªãåå©ç¨å¯è½ãªã³ã³ãã¼ãã³ããè¨è¨ãã¦ããã¨ãã«ç¹ã«ä¾¿å©ã§ãã
ä¾ãã°ããªã¹ãã®ã¬ã¤ã¢ã¦ãã¨çµãè¾¼ã¿ãã¸ãã¯ãå«ã <todo-list>
ã³ã³ãã¼ãã³ããå®è£
ãã¦ããã¨ãã¾ã:
<ul>
<li
v-for="todo in filteredTodos"
v-bind:key="todo.id"
>
{{ todo.text }}
</li>
</ul>
ããããã® todo ã«å¯¾ããã³ã³ãã³ãããã¼ãã³ã¼ãã£ã³ã°ãã代ããã«ãtodo ãã¨ã«ã¹ããããä½æããã¹ãããããããã£ã¨ã㦠todo
ããã¤ã³ããããã¨ã§ã親ã³ã³ãã¼ãã³ãããå¶å¾¡ã§ããããã«ãã¾ã:
<ul>
<li
v-for="todo in filteredTodos"
v-bind:key="todo.id"
>
<!--
ããããã® todo ã®ããã®ã¹ããããããã `todo` ãªãã¸ã§ã¯ãã
ã¹ãããã®ããããã£ã¨ãã¦æ¸¡ãã¦ãã
-->
<slot name="todo" v-bind:todo="todo">
<!-- ãã©ã¼ã«ããã¯ã³ã³ãã³ã -->
{{ todo.text }}
</slot>
</li>
</ul>
ãã® <todo-list>
ã³ã³ãã¼ãã³ããå©ç¨ããæãåããã®ãã¼ã¿ã«ã¯ã¢ã¯ã»ã¹ããªããããtodo ã¢ã¤ãã ã«å¯¾ãã¦ä»£ããã® <template>
ãå®ç¾©ãããã¨ãã§ãã¾ã:
<todo-list v-bind:todos="todos">
<template v-slot:todo="{ todo }">
<span v-if="todo.isComplete">â</span>
{{ todo.text }}
</template>
</todo-list>
ããããããã§ãã¹ã³ã¼ãä»ãã¹ããããå¯è½ã«ãããã¨ã®è¡¨é¢ãèµ°ãæ¸ãããç¨åº¦ã«ããã¾ãããå®ä¸çã®å¼·åãªå©ç¨ä¾ã«ã¤ãã¦ã¯ãVue Virtual Scroller ã Vue Promised, Portal Vue ã¨ãã£ãã©ã¤ãã©ãªãè¦ã¦ã¿ããã¨ããããããã¾ãã
éæ¨å¥¨ã®æ§æ
Vue 2.6.0 ã§
v-slot
ãã£ã¬ã¯ãã£ããå°å ¥ãããã¾ã ãµãã¼ãããã¦ãããã®ã®slot
ããã³slot-scope
å±æ§ã«ä»£ããæ¹åããã API ãæä¾ãã¦ãã¾ããv-slot
ãå°å ¥ããçç±ã¯ããã® RFC ã«å®å ¨ã«è¨è¿°ããã¦ãã¾ããslot
ããã³slot-scope
å±æ§ã¯ä»å¾ã® 2.x ãªãªã¼ã¹ã§å¼ãç¶ããµãã¼ãããã¾ãããå ¬å¼ã«éæ¨å¥¨ã¨ãªããVue 3 ã§ã¯åé¤ãããã§ãããã
slot
å±æ§ã«ããååä»ãã¹ããã
2.6.0 以éã§ã¯ éæ¨å¥¨ãæ¨å¥¨ãããæ°ããæ§æã«ã¤ãã¦ã¯ããã¡ã ãåç §ã
親ããååä»ãã¹ãããã«ã³ã³ãã³ãã渡ãã«ã¯ã<template>
ã«å¯¾ãã¦ç¹å¥ãª slot
å±æ§ã使ãã¾ã (ä¾ã¨ãã¦ããã ã§èª¬æãã <base-layout>
ã³ã³ãã¼ãã³ãã使ç¨):
<base-layout>
<template slot="header">
<h1>Here might be a page title</h1>
</template>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
<template slot="footer">
<p>Here's some contact info</p>
</template>
</base-layout>
ãããã¯ãslot
å±æ§ãé常ã®è¦ç´ ã«ç´æ¥æå®ãããã¨ãã§ãã¾ã:
<base-layout>
<h1 slot="header">Here might be a page title</h1>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
<p slot="footer">Here's some contact info</p>
</base-layout>
ååã®ãªãã¹ããã㯠ããã©ã«ãã¹ããã ã¨ãªããã¹ãããã®æå®ããªãã³ã³ãã³ãããã¹ã¦åãåãã¾ããä¸ã®ã©ã¡ãã®ä¾ããæç»ããã HTML ã¯æ¬¡ã®ããã«ãªãã¾ã:
<div class="container">
<header>
<h1>Here might be a page title</h1>
</header>
<main>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
</main>
<footer>
<p>Here's some contact info</p>
</footer>
</div>
slot-scope
å±æ§ã«ããã¹ã³ã¼ãä»ãã¹ããã
2.6.0 以éã§ã¯ éæ¨å¥¨ãæ¨å¥¨ãããæ°ããæ§æã«ã¤ãã¦ã¯ããã¡ã ãåç §ã
ã¹ãããã«æ¸¡ãããããããã£ãåãåãã«ã¯ã親ã³ã³ãã¼ãã³ã㯠<template>
ã«å¯¾ã㦠slot-scope
å±æ§ã使ãã¾ã (ä¾ã¨ãã¦ããã ã§èª¬æãã <slot-example>
ã使ç¨):
<slot-example>
<template slot="default" slot-scope="slotProps">
{{ slotProps.msg }}
</template>
</slot-example>
ããã§ãslot-scope
ã¯ããããã£ãåãåããªãã¸ã§ã¯ãã slotProps
å¤æ°ã¨ãã¦å®£è¨ãã<template>
ã®ä¸ã§å©ç¨ã§ããããã«ãã¦ãã¾ããJavaScript ã®é¢æ°å¼æ°ã®ååã¨åãããã«ãslotProps
ã®ä»£ããã«å¥½ããªååã使ããã¨ãã§ãã¾ãã
slot="default"
ã¯çç¥ãããã¨ãã§ãã¾ã:
<slot-example>
<template slot-scope="slotProps">
{{ slotProps.msg }}
</template>
</slot-example>
slot-scope
å±æ§ã¯ã<template>
è¦ç´ ä»¥å¤ (ã³ã³ãã¼ãã³ããå«ã) ã«å¯¾ãã¦ç´æ¥æå®ãããã¨ãã§ãã¾ã:
<slot-example>
<span slot-scope="slotProps">
{{ slotProps.msg }}
</span>
</slot-example>
slot-scope
ã®å¤ã¯ãé¢æ°å®ç¾©ã®å¼æ°é¨åã§æå¹ãªä»»æã® JavaScript å¼ãåãä»ãããã¨ãã§ãã¾ããããã¯ããµãã¼ãããã¦ããç°å¢ (åä¸ãã¡ã¤ã«ã³ã³ãã¼ãã³ã ã¾ã㯠ã¢ãã³ãã©ã¦ã¶) ã§ã¯ãå¼ã« ES2015 ã®åå²ä»£å
¥ ã使ãããã¨ãæå³ãã¾ã:
<slot-example>
<span slot-scope="{ msg }">
{{ msg }}
</span>
</slot-example>
ãã ã§èª¬æãã <todo-list>
ãä¾ã«ããã¨ãslot-scope
ã§åçã®ãã¨ãããã«ã¯æ¬¡ã®ããã«ãã¾ã:
<todo-list v-bind:todos="todos">
<template slot="todo" slot-scope="{ todo }">
<span v-if="todo.isComplete">â</span>
{{ todo.text }}
</template>
</todo-list>