vue 插槽slot

插槽是为组件空一个位置可以在需要的时候填上对应需要的标签内容

基本使用

<body>
<div id="app">
	<c1></c1>
	<c1><a href='www.baidu.com'>我是a</a></c1>
	<c1><i>我出来了</i></c1>
	<c1><button>点击</button></c1>
</div>
<template id='temp1'>
	<div style='border:1px solid black;width:20%'>
		<p>我是组件</p>
		<slot></slot>
	</div>
</template>	
<script>
const c1={
	template:'#temp1'
};
new Vue({
  el: '#app',
  components:{
  	c1
  }
})
</script>
</body>

在这里插入图片描述


给插槽设置默认值

<body>
<div id="app">
	<c1></c1>
	<c1><i>slot的默认值span变成了button</i></c1>
	<c1></c1>
</div>
<template id='temp1'>
	<div style='border:1px solid black;width:20%'>
		<slot><span>左边</span></slot>
	</div>
</template>	
<script>
const c1={
	template:'#temp1'
};
new Vue({
  el: '#app',
  components:{
  	c1
  }
})
</script>
</body>

在这里插入图片描述


具名插槽

一个组件里可以有多个插槽,当这些插槽在使用的时候需要修改的时候就要用具名插槽,即给插槽取一个name
当一个模板里有多个slot而在使用组件的时候只需要替换某一个具体的slot时,需要用name来关联

<body>
<div id="app">
	<c1>
		<button slot='middle'>哈哈</button>
	</c1>
</div>
<template id='temp1'>
	<div style='border:1px solid black;width:20%'>
		<slot name='left'><span>左边</span></slot>
		<slot name='middle'><span>中间</span></slot>
		<slot name='right'><span>右边</span></slot>
	</div>
</template>	
<script>
const c1={
	template:'#temp1'
};
new Vue({
  el: '#app',
  components:{
  	c1
  }
})
</script>
</body>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Brrby

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值