十一、スロット
45480 ワード
スロットの説明スロットは、Vueがコンテンツ配信を実現したAPIである. として機能する.コンポーネントがレンダリングされると、 に置き換えられます.
文字列
HTML
コンポーネント
に注意⚠️:
名前付きスロット という名前が隠されています.名前付きスロットにコンテンツを提供する場合、
要素:
要素は、コンテンツ配信を担持する出口
要素が挿入するコンテンツ(文字列、HTML、コンポーネント)文字列
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Documenttitle>
head>
<body>
<div id="app">
<hello-word>
hello-word>
div>
<template id='hello'>
<div>
<span> !span>
<slot>slot>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const HelloWord = {
template: '#hello'
}
new Vue({
el: '#app',
components: {
HelloWord
}
})
script>
body>
html>
HTML
<body>
<div id="app">
<hello-word>
<h1> h1>
hello-word>
div>
<template id='hello'>
<div>
<span> !span>
<slot>slot>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const HelloWord = {
template: '#hello'
}
new Vue({
el: '#app',
components: {
HelloWord
}
})
script>
body>
コンポーネント
<body>
<div id="app">
<hello-word>
<demo-scope>demo-scope>
hello-word>
div>
<template id='hello'>
<div>
<span> !span>
<slot>slot>
div>
template>
<template id='scope'>
<div>
?
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const HelloWord = {
template: '#hello'
}
const DemoScope = {
template: '#scope'
}
new Vue({
el: '#app',
components: {
HelloWord,
DemoScope
}
})
script>
body>
に注意⚠️:
要素がない場合、コンポーネントの開始ラベルから終了ラベルまでの間のコンテンツはすべて破棄されます.名前付きスロット
v-slot
はVue 2から指令する.6.0から導入され、より良いslot
およびslot-scope
の特性を提供するAPI代替スキームname
を持たない
要素には、「default」 v-slot
でv-slot
パラメータの でその を することができます.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Documenttitle>
head>
<body>
<div id="app">
<hello-word>
<template v-slot:header>
<div> div>
template>
<div> div>
<template v-slot:footer>
<div> div>
template>
hello-word>
div>
<template id='hello'>
<div>
<slot name="header">slot>
<slot>slot>
<slot name="footer">slot>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const HelloWord = {
template: '#hello'
}
new Vue({
el: '#app',
components: {
HelloWord
}
})
script>
body>
html>
アクティブドメインスロット
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Documenttitle>
head>
<body>
<div id="app">
<hello-word>
<template v-slot:header="scope">
<div>{{scope.user.first}}div>
template>
<div> div>
<template v-slot:footer>
<div> div>
template>
hello-word>
div>
<template id='hello'>
<div>
<slot name="header" :user="msg">slot>
<slot>slot>
<slot name="footer">slot>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
//
const HelloWord = {
template: '#hello',
data () {
return {
msg: {
first: ' ! 。。。',
last: ' ! 。。。'
}
}
}
}
new Vue({
el: '#app',
components: {
HelloWord
}
})
script>
body>
html>
ドメインスロットの :まず、 のコードには2つの なる ドメインが することを する がある.1つは、HelloWord
の を いるVueインスタンスと する ドメインであり、もう1つは、HelloWord
という に するVueインスタンスと する ドメイン である. に、この2つの ドメインが いに されていることを り、さらに の を えないと、 いにVueインスタンスにアクセスできないため、data
、methods
のようなVueインスタンスが つメンバーがすべて できないことは うまでもない. に づいて、この2つの ドメインが いにいくつかの を ることができる を する がある 、いくつかの を らなければなりません.この には、 ドメインスロットが まれています. に、この2つの ドメインは、HelloWord
コンポーネントを いたVue ドメインが ドメインであり、コンポーネントHelloWord
のVue ドメインが ドメインである があると えられる.さらに、 を2つの に けることができます. は の を っていて、 は の を っています. したがって、 ドメインスロットの の は、 に の を らせることである.
スロットpropとは
にバインドされた をスロットpropと ぶ. コード:user="msg"
がスロットprop
スロットprop<hello-word>
<template v-slot:header="{ user }">
<div>{{ user.first }}div>
template>
<div> div>
<template v-slot:footer>
<div> div>
template>
hello-word>
prop <hello-word>
<template v-slot:header="{ user: person }">
<div>{{ person.first }}div>
template>
<div> div>
<template v-slot:footer>
<div> div>
template>
hello-word>
スロットのまとめスロットの は、 のテンプレート( 、HTML、コンポーネント) であってもよい. 2.6.0 しい きスロットは、
のnameプロパティとv-slot
によって、スロットのオンデマンドロードと ロード を します. ⚠️: v-slot
は、 ,v-slot
に に く があります(#)スロットは に できます:サブアセンブリに を って、 の に を くかは アセンブリによって まります ドメインスロットの は、 に の を らせる である. v-slot
は(#)x` と できる.