组件的插槽
举例:
<!--
1. 插槽的基本使用 <slot></slot>
2.插槽的默认值:<slot>button</slot>
3.如果有多个值,同时放入到组件进行替换时,一起作为替换元素
-->
<div id="app">
<cpn><button>按钮</button></cpn>
<cpn></cpn>
<cpn><span>标签</span></cpn>
</div>
<template id="cpn">
<div>
<h2>我是组件</h2>
<p>666</p>
<slot><button>默认为按钮</button></slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "你好,有勇气的牛排"
},
components: {
cpn: {
template: '#cpn'
}
}
})
</script>
<div id="app">
<cpn><span slot="center">标题</span></cpn>
<cpn><button slot="center">标题</button></cpn>
</div>
<template id="cpn">
<div>
<slot name="left"><span>左边</span></slot>
<slot name="center"><span>中间</span></slot>
<slot name="right"><span>右边</span></slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "你好,有勇气的牛排"
},
components: {
cpn: {
template: '#cpn'
}
}
})
</script>
<div id="app">
<cpn></cpn>
<cpn>
<!-- 目的是获取子组件中的pLanguages -->
<template v-slot="slot">
<span v-for="item in slot.data">{{ item }} * </span>
</template>
</cpn>
<cpn>
<!-- 目的是获取子组件中的pLanguages -->
<!-- .join 将数组,转为字符串 -->
<template v-slot="slot">
<span>{{ slot.data.join(' * ') }}</span>
</template>
</cpn>
</div>
<template id="cpn">
<div>
<slot :data="pLanguages">
<ul>
<li v-for="item in pLanguages">{{ item }}</li>
</ul>
</slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "你好,有勇气的牛排"
},
components: {
cpn: {
template: '#cpn',
data() {
return {
pLanguages: ['JavaScript', 'C++', 'Java', 'C#', 'Python']
}
}
}
}
})
</script>
仓库地址:## 1 为什么使用slot
组件的插槽
举例:
<!--
1. 插槽的基本使用 <slot></slot>
2.插槽的默认值:<slot>button</slot>
3.如果有多个值,同时放入到组件进行替换时,一起作为替换元素
-->
<div id="app">
<cpn><button>按钮</button></cpn>
<cpn></cpn>
<cpn><span>标签</span></cpn>
</div>
<template id="cpn">
<div>
<h2>我是组件</h2>
<p>666</p>
<slot><button>默认为按钮</button></slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "你好,有勇气的牛排"
},
components: {
cpn: {
template: '#cpn'
}
}
})
</script>
<div id="app">
<cpn><span slot="center">标题</span></cpn>
<cpn><button slot="center">标题</button></cpn>
</div>
<template id="cpn">
<div>
<slot name="left"><span>左边</span></slot>
<slot name="center"><span>中间</span></slot>
<slot name="right"><span>右边</span></slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "你好,有勇气的牛排"
},
components: {
cpn: {
template: '#cpn'
}
}
})
</script>
<div id="app">
<cpn></cpn>
<cpn>
<!-- 目的是获取子组件中的pLanguages -->
<template v-slot="slot">
<span v-for="item in slot.data">{{ item }} * </span>
</template>
</cpn>
<cpn>
<!-- 目的是获取子组件中的pLanguages -->
<!-- .join 将数组,转为字符串 -->
<template v-slot="slot">
<span>{{ slot.data.join(' * ') }}</span>
</template>
</cpn>
</div>
<template id="cpn">
<div>
<slot :data="pLanguages">
<ul>
<li v-for="item in pLanguages">{{ item }}</li>
</ul>
</slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "你好,有勇气的牛排"
},
components: {
cpn: {
template: '#cpn',
data() {
return {
pLanguages: ['JavaScript', 'C++', 'Java', 'C#', 'Python']
}
}
}
}
})
</script>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。