Vue-52、Vue技术插槽使用

1、默认插槽

App.vue

<template>
    <div class="container">
        <category title="美食">
            <img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.F0xLT-eKcCSOI5tdjBv9FAHaE8?w=303&h=202&c=7&r=0&o=5&pid=1.7">
        </category>
        <category title="游戏"  >
            <ul>
                <li v-for="(p,index) in games" :key="index">{
   {
   p}}</li>
            </ul>
        </category>
        <category title="电影">
            <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
        </category>
    </div>
</template>
<script>
    import Category from './components/Category'
    export default {
   
        name: 'App',
        components: {
   
            Category
        },
        data(){
   
            return{
   
                foods:['火锅','烧烤','小龙虾','牛排'],
                games:['红色警戒','穿越火线','劲舞团','超近玛丽'],
                films:['《教父》','《拆弹专家》','《你好李焕英》','《龙门客栈》'],
            }
        }
    }
</script>
<style scoped>
    .container{
   
        display: flex;
        justify-content: space-around;
    }
    img{
   
        width: 120%;
        margin-left: -36px;
    }
    video{
   
        width: 120%;
        margin-left: -36px;
    }
</style>


Category.vue

<template>
    <div class="category">
        <h3>{
   {
   title}}分类</h3>
        <ul>
            <!--定义一个插槽(挖个坑,等着组件的使用者进行填充)-->
            <slot>我是一个默认值,当使用者没有传递具体结构时,我会出现</slot>
        </ul>
    </div>
</template>

<script>
    export default {
   
        name: "Category",
        props:['title'],
        data(){
   
            return{
   

            }
        }
    }
</script>

<style scoped>
    .category{
   
        background-color: skyblue;
        width: 200px;
        height: 300px;
    }

    h3{
   
        text-align: center;
        background-color: orange;
    }
</style>

效果
在这里插入图片描述
2、具名插槽

App.vue

<template>
    <div class="container">
        <category title="美食">
            <img slot="center" src="https://tse2-mm.cn.bing.net/th/id/OIP-C.F0xLT-eKcCSOI5tdjBv9FAHaE8?w=303&h=202&c=7&r=0&o=5&pid=1.7">
            <a slot="footer" href="https://www.baidu.com">更多美食</a>
        </category>
        
        <category title="游戏"  >
            <ul slot="center">
                <li v-for="(p,index) in games" :key="index">{
   {
   p}}</li>
            </ul>
          <div class="foot" slot="footer">
              <a  href="https://www.baidu.com">单机游戏</a>
              <a  href="https://www.baidu.com">网络游戏</a>
          </div>
        </category>
        <category title="电影"   slot="footer">
            <video  slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
            <div slot="footer" class="foot">
                <a  href="https://www.baidu.com">经典</a>
                <a  href="https://www.baidu.com">热门</a>
                <a  href="https://www.baidu.com">推荐</a>
            </div>
            <h4 slot="footer">欢迎来看电影</h4>
        </category>
    </div>
</template>
<script>
    import Category from './components/Category'
    export default {
   
        name: 'App',
        components: {
   
            Category
        },
        data(){
   
            return{
   
                foods:['火锅','烧烤','小龙虾','牛排'],
                games:['红色警戒','穿越火线','劲舞团','超近玛丽'],
                films:['《教父》','《拆弹专家》','《你好李焕英》','《龙门客栈》'],
            }
        }
    }
</script>
<style scoped>
    .container{
   
        display: flex;
        justify-content: space-around;
    }
    img{
   
        width: 120%;
        margin-left: -36px;
    }
    video{
   
        width: 120%;
        margin-left: -36px;
        /*margin-top: 80px;*/
    }
    .foot{
   
        display: flex;
        justify-content: space-around;

    }
    h4{
   
        text-align: center;
    }

</style>

Category.vue

<template>
    <div class="category">
        <h3>{
   {
   title}}分类</h3>
        <ul>
            <!--定义一个插槽(挖个坑,等着组件的使用者进行填充)-->
            <slot name="center">我是一个默认值,当使用者没有传递具体结构时,我会出现</slot>
            <slot name="footer">我是一个默认值,当使用者没有传递具体结构时,我会出现</slot>
        </ul>
    </div>
</template>

<script>
    export default {
   
        name: "Category",
        props:['title'],
        data(){
   
            return{
   

            }
        }
    }
</script>

<style scoped>
    .category{
   
        background-color: skyblue;
        width: 200px;
        height: 300px;
    }

    h3{
   
        text-align: center;
        background-color: orange;
    }
</style>

效果
在这里插入图片描述
3、作用域插槽

Category.vue

<template>
    <div class="category">
        <h3>{
   {
   title}}分类</h3>
            <!--定义一个插槽(挖个坑,等着组件的使用者进行填充)-->
            <slot :games="games">我是一个默认值,当使用者没有传递具体结构时,我会出现</slot>
    </div>
</template>
<script>
    export default {
   
        name: "Category",
        props:['title'],
        data(){
   
            return{
   
                games:['红色警戒','穿越火线','劲舞团','超近玛丽'],
            }
        }
    }
</script>

<style scoped>
    .category{
   
        background-color: skyblue;
        width: 200px;
        height: 300px;
    }

    h3{
   
        text-align: center;
        background-color: orange;
    }
</style>

第一种写法

<category title="游戏"  >
            <template scope="{games}">
                <ul>
                    <li v-for="(p,index) in games" :key="index">{
   {
   p}}</li>
                </ul>
            </template>
        </category>

第二种写法

 <category title="游戏"  >
            <template slot-scope="{games}">
                <ol>
                    <li v-for="(p,index) in games" :key="index">{
   {
   p}}</li>
                </ol>
            </template>
        </category>

第三种写法

<category title="游戏"  >
            <template scope="ati">
                <h4 v-for="(p,index) in ati.games" :key="index">{
   {
   p}}</h4>
            </template>
        </category>

效果
在这里插入图片描述
总结

1、作用:让父组件可以向子组件制定位置插入html结构,也是一种组件的通信方式,适用于父组件====>子组件
2、分类:默认插槽、具名插槽、作用域插槽

3、使用方式

	1、默认插槽
父组件中:
<Category>
	<div>html</div/>
</Category>

子组件中:
<template>
	<div>
		<slot>
			插槽默认内容
		</slot>
	</div/>
</template>
	2、具名插槽
父组件中:

<Category>
	<template slot="footer">
		<div>html结构1</div/>
	</template>
	
	<template slot="center">
		<div>html结构2</div/>
	</template>

</Category>

子组件中:
<template>
	<div>
		<slot name="center">插槽默认内容</slot>
		<slot name="footer">插槽默认内容</slot>
	</div/>
</template>
	3、作用域插槽
			
		1、理解:数组在组件的自身,但根据数据生成的结构需要组件的使用者来决定。(games数据在Category组件中,但是使用数据所遍历出来的结构由App组件决定)

		2、具体编码:
父组件中:
<Category>
	<template  scope="scopeData">
		<ul>
			<li v-for="(p,index) in scopeData.games" :key="index">{
   {
   p}}</li>
		</ul>
	</template>
</Category>

<Category>
	<template  slot-scope="scopeData">
		<ul>
			<h4 v-for="(p,index) in scopeData.games" :key="index">{
   {
   p}}</h4>
		</ul>
	</template>
</Category>


子组件:

<template>
	<div>
		<slot :games="games">插槽默认内容</slot>
	</div/>
</template>
<script>
    export default {
   
        name: "Category",
        props:['title'],
        data(){
   
            return{
   
                games:['红色警戒','穿越火线','劲舞团','超近玛丽'],
            }
        }
    }
</script>

相关推荐

  1. vue 使用

    2024-02-03 06:42:06       13 阅读
  2. vue slot使用

    2024-02-03 06:42:06       17 阅读
  3. Vue 3

    2024-02-03 06:42:06       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 06:42:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 06:42:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 06:42:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 06:42:06       18 阅读

热门阅读

  1. 学习方法分享

    2024-02-03 06:42:06       34 阅读
  2. Python数据库编程:SQLite、MySQL与MongoDB

    2024-02-03 06:42:06       31 阅读
  3. NetApp FAS2750 和 FAS2820 简化分布式企业的存储

    2024-02-03 06:42:06       32 阅读
  4. 【MQ04】消息持久化与确认机制

    2024-02-03 06:42:06       24 阅读
  5. MacOS安装dmg提示已文件已损坏的解决方法

    2024-02-03 06:42:06       37 阅读
  6. ResizeObserver视图观察器

    2024-02-03 06:42:06       33 阅读
  7. 部署私有知识库项目FastGPT

    2024-02-03 06:42:06       25 阅读