slot插槽

slot简单的说就是父组件想在要往子组件里面插入一点内容

slot分为三种:基本使用、作用域插槽、具名插槽


基本使用:

<!-- 父组件 -->
<template>
	<div>
		<!-- 子组件 -->
		<SlotDemo :url="website.url">
			{{website.title}}
		</SlotDemo>
	</div>
</templte>

<script>
	import SlotDemo from "./SlotDemo";
	export default {
		components:{
			SlotDemo
		},
		data(){
			return{
				name:"哈哈",
				website:{
					url:"www.baidu.com",
					title:'0v0',
					subTitle:"程序员的梦工厂"
				}
			}
		}
	}
</script>
<!-- 子组件 -->
<template>
		<a :href="url">
			<slot>
			   默认内容,父组件没有设置内容的时候,这里显示
			</slot>
		</a>
</templte>

<script>
	export default {
		props:['url'], //这是属性的一个简写
		data(){
		return{ 

		}
	  }
	}
</script>

作用域插槽:

<!-- 父组件 -->
<template>
	<div>
		<!-- 子组件 -->
		<ScopedSlotDemo :url="website.url">
		<!--slotProps这个名字是可以自己定义的-->
			<template v-slot="slotProps">
				{{slotProps.slotData.title }}
			</template>
		</ScopedSlotDemo>
	</div>
</templte>

<script>
	import ScopedSlotDemo from "./SlotDemo";
	export default {
		components:{
			ScopedSlotDemo
		},
		data(){
			return{
				name:"哈哈",
				website:{
					url:"www.baidu.com",
					title:'0v0',
					subTitle:"程序员的梦工厂"
				}
			}
		}
	}
</script>
<!-- 子组件 -->
<template>
		<!--slotData这个名字是可以自己定义的-->
		<a :slotData="website">
			<slot>
			   默认内容,父组件没有设置内容的时候,这里显示
			</slot>
		</a>
</templte>

<script>
	export default {
		props:['url'], //这是属性的一个简写
		data(){
		return{ 
			website:{
				url:"www.zizujian.com",
				title:'mom',
				subTitle:"子组件website"
			}
		}
	  }
	}
</script>

具名插槽:

	<!-- 父组件 -->
	
	<NamedSlot>
		<!-- 缩写的话就是 <template #header> -->
		<template v-slot:header>
			<h1>请插入 header slot 中</h1>
		</template>
		
		<p>将插入到main slot 中,即未命名的 slot</p>

		<template v-slot:footer>
			<h1>请插入 footer slot 中</h1>
		</template>
	</NamedSlot>
<!-- NamedSlot组件 -->
<div class="container">
	<header>
		<slot name="header"></slot>
	</header>
	<mian>
		<slot></slot>
	</mian>
	<footer>
		<slot name="footer"></slot>
	</footer>
</div>
### Vue.js Slot 插槽使用教程 #### 默认插槽 默认插槽是最简单的形式,它允许父组件向子组件传递任意内容。当子组件中只有一个未命名的 `<slot>` 标签时,默认插槽会自动接收来自父组件的内容。 ```html <!-- 子组件 ChildComponent.vue --> <template> <div class="child"> <h2>这是子组件</h2> <slot></slot> </div> </template> <script> export default { name: 'ChildComponent' } </script> ``` ```html <!-- 父组件 ParentComponent.vue --> <template> <div class="parent"> <h1>这是父组件</h1> <ChildComponent> 这里是通过默认插槽传入的内容。 </ChildComponent> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent } } </script> ``` #### 具名插槽 具名插槽允许多个不同的位置插入内容。通过给 `<slot>` 添加 `name` 属性,在父组件中可以指定具体哪个具名插槽应该被填充。 ```html <!-- 子组件 NamedSlotsExample.vue --> <template> <div class="named-slots-example"> <header> <slot name="header"></slot> </header> <main> <slot></slot> </main> <footer> <slot name="footer">这里是备用内容。</slot> </footer> </div> </template> ``` ```html <!-- 父组件 UsingNamedSlots.vue --> <template> <div class="using-named-slots"> <NamedSlotsExample> <!-- 向 header 名字的 slot 填充内容 --> <template v-slot:header> <h1>这里是从父级传来的头部内容</h1> </template> <!-- 默认插槽内容 --> <p>这是一些普通的段落文本。</p> <!-- 向 footer 名字的 slot 填充内容 --> <template #footer> <p>这里是从父级传来的底部内容</p> </template> </NamedSlotsExample> </div> </template> <script> import NamedSlotsExample from './NamedSlotsExample.vue'; export default { components: { NamedSlotsExample }, }; </script> ``` #### 作用域插槽 作用域插槽让子组件能够将数据暴露给父组件使用的模板片段内。这意味着可以在父组件中访问到子组件的数据并基于这些数据显示自定义内容。 ```html <!-- 子组件 ScopedSlotExample.vue --> <template> <ul> <li v-for="(item, index) in items" :key="index"> <slot :text="item.text">{{ item.text }}</slot> </li> </ul> </template> <script> export default { data() { return { items: [ { text: "苹果" }, { text: "香蕉" }, { text: "橙子" } ] }; } } </script> ``` ```html <!-- 父组件 UseScopedSlot.vue --> <template> <div class="use-scoped-slot"> <ScopedSlotExample v-slot:default="{ text }"> {{ text }} </ScopedSlotExample> </div> </template> <script> import ScopedSlotExample from "./ScopedSlotExample"; export default { components: { ScopedSlotExample } }; </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值