vue3学习笔记(13)插槽slot

Vue3专栏入口

1.实现需求

一个组件,实现以下三个不同的需求这时就需要在组件总用到插槽标签slot
在这里插入图片描述

2.目录结构

在这里插入图片描述

3.完整代码

Category.vue完整代码:

<template>
    <div class="category">
        <h3>{{title}}</h3>
        <slot></slot>
    </div>
</template>

<script>

import { defineComponent } from 'vue'

export default defineComponent({
    name:'Category',
    props:['title'],
    setup(props,context){

    }
})
</script>

<style scoped>
.category{
    width: 200px;
    height: 300px;
    background-color: aquamarine;
}
h3{
    text-align: center;
    background-color: rgb(155, 190, 16);
}
</style>

app.vue完整代码:

<template>
	<div class="category_div">
		<Category title="动漫">
			<img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F1003%2F7abe7513g00r0ekle0075d2009o00amg009o00am.gif&refer=http%3A%2F%2Fdingyue.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656771898&t=fdacac50f4c0988b3822f333207eb492" alt="天天">
		</Category>

		<Category title="游戏">
			<ul>
				<li v-for="(g,index) in gameList" :key="index">{{g}}</li>
			</ul>
		</Category >

		<Category title="电影">
			<h1>没有视频就用文字代替吧</h1>
		</Category>
	</div>

</template>

<script lang="ts">

import { defineComponent } from 'vue'
import Category from './components/Category.vue'
export default defineComponent({
	name: 'App',
	components: { Category },
	setup(props, context) {
		const gameList = ["红警","LOL","CF","DNF"]
		return{
			gameList
		}
	}
})
</script>

<style scoped>
.category_div {
	background-color: beige;
	width: 800px;
	display: flex;
	justify-content: space-around;
}
img{
	width: 100%;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中的作用域插槽是一种功能强大的特性,它允许你在父组件中向子组件传递内容,并且还能让子组件对这些内容进行处理和渲染。 使用作用域插槽,你可以在子组件中定义插槽的具体渲染规则,并且可以在父组件中通过具名插槽的方式向子组件传递内容。子组件可以通过`v-slot`指令来接收父组件传递的内容,并在插槽内部进行渲染。 在Vue 3中,作用域插槽的语法有所改变。你可以使用`<slot>`元素加上`name`属性来定义具名插槽,在父组件中使用`v-slot`指令来向该具名插槽传递内容。例如: ```html <!-- 子组件 --> <template> <div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template> <!-- 父组件 --> <template> <div> <child-component> <template v-slot:header> <!-- 这里的内容将会传递给子组件的具名插槽header --> </template> <!-- 这里的内容将会传递给子组件的默认插槽 --> <template v-slot:footer> <!-- 这里的内容将会传递给子组件的具名插槽footer --> </template> </child-component> </div> </template> ``` 在Vue 3中,还支持使用缩写语法来定义默认插槽和具名插槽。例如,`<template v-slot:header>`可以简写为`<template #header>`,`<template v-slot:footer>`可以简写为`<template #footer>`。 通过使用作用域插槽,你可以更灵活地向子组件传递内容,并让子组件有更大的自由度来处理和渲染这些内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值