Vue 中插槽的使用总结

目录
  • 默认插槽
  • 具名插槽
  • 作用域插槽
  • 插槽总结

默认插槽

首先做一个页面:

新增 Category.vue

<template>
<div class="category">
  <h3>{{title}}分类</h3>
  <ul>
    <li v-for="(item,index) in listData" :key="index">{{item}}</li>
  </ul>
</div>
</template>

<script>
export default {
  name: "Category",
  props:["title","listData"]
}
</script>

<style scoped>
.category{
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3{
  text-align: center;
  background-color: orange;
}
</style>

App.vue 中使用

<template>
  <div class="container">
    <Category title="美食" :listData="foods"/>
    <Category title="游戏" :listData="games"/>
    <Category title="电影" :listData="films"/>
  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
  data(){
    return{
      foods:["火锅","烧烤","小龙虾","牛排"],
      games:["劲舞团","QQ飞车","超级玛丽","无人深空"],
      films:["《教父》","《狩猎》","《禁闭岛》","《聚焦》"]
    }
  }

}
</script>

<style>
.container {
  display: flex;
  justify-content: space-around;
}
</style>

现在修改需求,每个分类都要展示不同的内容:

这里就用到了插槽,修改 Category.vue

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

<script>
export default {
  name: "Category",
  props:["title"]
}
</script>

<style scoped>
.category{
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3{
  text-align: center;
  background-color: orange;
}
</style>

修改 App.vue

<template>
  <div class="container">
    <Category title="美食" :listData="foods">
      <img src="https://img2.baidu.com/it/u=2073611229,1921777437&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675"/>
    </Category>
    <Category title="游戏" :listData="games">
      <ul>
        <li v-for="(g,index) in games" :key="index">{{g}}</li>
      </ul>
    </Category>
    <Category title="电影">
      <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"/>
    </Category>
  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
  data(){
    return{
      foods:["火锅","烧烤","小龙虾","牛排"],
      games:["劲舞团","QQ飞车","超级玛丽","无人深空"],
      films:["《教父》","《狩猎》","《禁闭岛》","《聚焦》"]
    }
  }

}
</script>

<style>
.container {
  display: flex;
  justify-content: space-around;
}
img,video{
  width: 100%;
}
</style>

具名插槽

修改 Category.vue

<template>
<div class="category">
  <h3>{{title}}分类</h3>
  <slot name="center">这是一些默认值,没有内容时展示</slot>
  <slot name="footer">这是一些默认值,没有内容时展示</slot>
</div>
</template>

<script>
export default {
  name: "Category",
  props:["title"]
}
</script>

<style scoped>
.category{
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3{
  text-align: center;
  background-color: orange;
}
</style>

修改 App.vue

<template>
  <div class="container">
    <Category title="美食" :listData="foods">
      <img slot="center" src="https://img2.baidu.com/it/u=2073611229,1921777437&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675"/>
      <a slot="footer" href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >更多美食</a>
    </Category>
    <Category title="游戏" :listData="games">
      <ul slot="center">
        <li v-for="(g,index) in games" :key="index">{{g}}</li>
      </ul>
      <div class="foot" slot="footer">
        <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >单机游戏</a>
        <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >网络游戏</a>
      </div>
    </Category>
    <Category title="电影">
      <video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"/>
      <!--v-slot 只有template能用-->
      <template v-slot:footer>
        <div class="foot" slot="footer">
          <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >经典</a>
          <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >热门</a>
          <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >推荐</a>
        </div>
      </template>
    </Category>
  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
  data(){
    return{
      foods:["火锅","烧烤","小龙虾","牛排"],
      games:["劲舞团","QQ飞车","超级玛丽","无人深空"],
      films:["《教父》","《狩猎》","《禁闭岛》","《聚焦》"]
    }
  }

}
</script>

<style>
.container,.foot {
  display: flex;
  justify-content: space-around;
}
img,video{
  width: 100%;
}
</style>

作用域插槽

如果数据在 Category 中,但需要展示不同的形式,我们可以通过插槽传值,类似于我们从父组件向子组件传值:

<template>
  <div class="category">
    <h3>{{ title }}分类</h3>
    <slot :games="games" :msg="hello"></slot>
  </div>
</template>

<script>
export default {
  name: "Category",
  props: ["title"],
  data() {
    return {
      games: ["劲舞团", "QQ飞车", "超级玛丽", "无人深空"]
    }
  }
}
</script>

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

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

App 中在子组件中使用 <template> 包裹要展示的内容,标签中可以使用scope接收传过来的值

<template>
  <div class="container">
    <Category title="游戏">
      <template v-slot="receiveValue">
        <ul>
          <li v-for="(g,index) in receiveValue.games" :key="index">{{ g }}</li>
        </ul>
        <a>{{ receiveValue.msg }}</a>
      </template>
    </Category>

    <Category title="游戏">
      <template v-slot="{games,msg}">
        <ol>
          <li v-for="(g,index) in games" :key="index">{{ g }}</li>
        </ol>
        <a>{{ msg }}</a>
      </template>
    </Category>

    <Category title="游戏">
      <template v-slot="{games,msg}">
        <h4 v-for="(g,index) in games" :key="index">{{ g }}</h4>
        <a>{{ msg }}</a>
      </template>
    </Category>

  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
}
</script>

<style>
.container, .foot {
  display: flex;
  justify-content: space-around;
}

img, video {
  width: 100%;
}
</style>

插槽总结

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

默认插槽

父组件中:

<Category>
    <div>html结构</div>
<Category>

子组件中:

<template>
    <div>
    <!--定义插槽-->
    <slot>插槽默认内容...</slot>
    </div>
</templdte>

具名插槽

父组件中:

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

子组件中:

<template>
    <div>
    <1--定义插槽-->
    <slot name="center">插槽默认内容...</slot>
    <slot name="footer”>插槽默认内容...</slot>
    </div>
</template>

作用域插槽

  • 1.理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定,(games 数据在 Category 组件中,但使用数据所遍历出来的结构由 App 组件决定)
  • 2.具体编码:

父组件中:

<category>
    <template v-slot="scopeData"
    <!--生成的是ul列表-->
    <ul>
        <li v-for="g in scopeDsta.games" : key="g">{g}</li>
    </ul>
    </template>
</Category>

<Category>
    <template v-slot={scopeData}>
    <!--生成的是h4标题-->
    <h4 v-for="g in scopeData" :key="g">{{g}}</h4>
    </template>
</Category>

子组件中:

<template>
    <div>
        <slot :games="games"></slot>
    </div>
</template>

<script>
export default{
    name: "Category ',
    props: ["title"],
    //数据在子组件自身
    data() {
        return{
            games:['红色警戒,穿越火线',"劲舞团",超级玛丽"]
        }
    }
}</script>

注意:scope和slot-scope过时了,可以使用v-slot

到此这篇关于Vue 中插槽的使用总结的文章就介绍到这了,更多相关Vue 插槽使用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Vue默认插槽,具名插槽,作用域插槽定义及使用方法

    目录 一.三种插槽的定义 1.默认插槽 2.具名插槽 3.作用域插槽 二.使用方法 1.默认插槽 2.具名插槽 3.作用域插槽 应用场景: 插槽的作用是在子组件中某个位置插入父组件的自定义html结构和data数据 一.三种插槽的定义 插槽分为三种: 默认插槽 具名插槽 作用域插槽 1.默认插槽 [定义:默认插槽是将父组件的结构和数据插入子组件中,默认插槽只有一个插入位置,要插入的html结构和data数据必须在父组件中,不过css可以在子组件中][简述:将父组件的自定义html和data插入子

  • vue+element-ui表格封装tag标签使用插槽

    我们知道有很多系统都要求表格中添加各种各样的tag,来标记一些属性.在element-ui中添加tag很简单,最重要的就是用到了vue的插槽slot这个特性.首先了解什么是插槽. 插槽 省去官方的复杂讲解和代码,插槽的意思简单来说,就是在子组件的某个地方留一个占位符,当父组件使用这个子组件的时候,可以自定义这个占位符所占地方呈现的样子,可能是一个标题,一个按钮,甚至一个表格,一个表单. 为什么要插槽呢?我们抽离组件的原因就是因为可重复的代码太多了,当使用可复用的组件时,大大减少了复制粘贴.设想有

  • Vue中插槽slot的使用方法与应用场景详析

    什么是插槽? 我们知道在Vue中 Child 组件的标签 的中间是不可以包着什么的 . 可是往往在很多时候我们在使用组件的时候总想在组件间外面自定义一些标签,vue新增了一种插槽机制,叫做作用域插槽.要求的版本是2.1.0+: 插槽,其实就相当于占位符.它在组件中给你的HTML模板占了一个位置,让你来传入一些东西.插槽又分为 匿名插槽.具名插槽.作用域插槽. 在 2.6.0 中,我们为具名插槽和作用域插槽引入了一个新的统一的语法 (即 v-slot 指令).它取代了 slot 和 slot-sc

  • Vue slot插槽的使用详情

    目录 1.为什么使用slot 1.1 slot(插槽) 1.2 组件中的插槽 1.3 例子 2.如何封装这类组件(slot) 3. 插槽的案例 4.插槽默认值 5.具名插槽 6.编译作用域 7.作用域插槽 1.为什么使用slot 1.1 slot(插槽) 在生活中很多地方都有插槽,电脑usb的插槽,插板当中的电源插槽 插槽的目的是为了让我们原来的设备具备更多的扩展性 比如电脑的USB我们可以插入U盘,手机,鼠标,键盘等等 1.2 组件中的插槽 组件的插槽也是为了让我们的组件更具有扩展性 让使用者

  • vue2中插槽(slot)的基本使用规范

    目录 前言 基础slot组件(匿名插槽) 具名插槽 作用域插槽 解构插槽 总结 前言 在vue的开发过程中,我们会经常使用到vue的slot插槽组件,vue官方文档的描述: Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案,将元素作为承载分发内容的出口 slot大概分为以下几种: 基础slot组件(匿名插槽) 匿名插槽主要使用场景并不涉及特别复杂的业务,更像是纯展示组件内容 <!--子组件--> <template>   

  • Vue3 插槽使用汇总

    目录 一.v-slot 介绍 二.匿名插槽 三.具名插槽 四.作用域插槽 五.动态插槽名 一.v-slot 介绍 v-slot 只能用在 template 或组件上使用,否则就会报错. v-slot 也是其中一种指令. 使用示例: //父组件代码 <child-com> <template v-slot:nameSlot> 插槽内容 </template> </child-com> //组件模板 <slot name="nameSlot&qu

  • 浅谈Vue中插槽slot的使用方法

    如何定义和使用: 在组件的template中使用slot标签定义,slot标签中间可以定义默认显示值,如果slot标签没有声明name属性值,在使用插槽时将默认从第一个插槽依次往下放置,为了方便使用,一般都会都插槽slot指定一个name属性值,当要使用该插槽时,只需要在要使用的标签内添加slot='插槽名字',就可以将指定的标签放到指定的插槽内,插槽内可以是任意内容. 举例: <!DOCTYPE html> <html lang="en"> <head&

  • Vue中插槽和过滤器的深入讲解

    目录 插槽 什么是插槽? 插槽内容 编译作用域 后备内容 具名插槽 过滤器 概念 语法 全局过滤器 局部过滤器 练习 总结 插槽 什么是插槽? 概念 Vue 实现了一套内容分发的 API,为组件提供了一个 <slot> 元素作为承载分发内容的出口. 简单来说就是<slot> 元素作为组件模板之中的内容分发插槽.<slot> 元素自身将被替换. 插槽内容 语法 首先先新建一个文件来书写我们的slot // slot.vue <template> <div

  • vue中插槽整理及用法分析

    本教程操作环境:windows7系统.vue2.9.6版,DELL G3电脑. vue的插槽(slot)主要分三种: 默认插槽,具名插槽,作用域插槽 vue中的插槽,指的是子组件中提供给父组件使用的一个占位符: 用标签表示,父组件可以在这个占位符中填充任何模板代码,比如HTML.组件等,填充的内容会替换掉子组件的标签(替换占位符). 默认插槽 默认插槽是最简单的一种插槽,和上面的描述一致,就是通过替换占位符达到在父组件中更改子组件中内容的效果. 语法: 在子组件中放置一个占位符(slot): <

  • Vue 中插槽的使用总结

    目录 默认插槽 具名插槽 作用域插槽 插槽总结 默认插槽 首先做一个页面: 新增 Category.vue <template> <div class="category">   <h3>{{title}}分类</h3>   <ul>     <li v-for="(item,index) in listData" :key="index">{{item}}</li&g

  • Vue中插槽Slot基本使用与具名插槽详解

    目录 一.插槽Slot 1.1.插槽Slot的作用 1.2.具名插槽Slot Ps:作用域插槽 总结 一.插槽Slot 1.1.插槽Slot的作用 初识插槽: 为了让这个组件具备更强的通用性,我们不能将组件中的内容限制为固定的div.span等等这些元素: 比如某种情况下我们使用组件,希望组件显示的是一个按钮,某种情况下我们使用组件希望显示的是一张图片: 我们应该让使用者可以决定某一块区域到底存放什么内容和元素: 所以就可以使用插槽来解决这个问题 换句话说就是,我们要是想在一个组件标签中添加新的

  • Vue中插槽slot的使用方法

    目录 1.什么是插槽 2.插槽的使用 3.v-slot指令 4.具名插槽 5.具名插槽的简写形式 6.作用域插槽 7.解构插槽 Prop 1.什么是插槽 插槽(slot)是 vue 为组件的封装者提供的能力.允许开发者在封装组件时,把不确定的.希望由用户 指定的部分定义为插槽. 2.插槽的使用 在封装组件时,可以通过 元素定义插槽,从而为用户预留内容占位符. // 子组件 <template> <div class="left-container"> <h

  • Vue中slot的使用详解

    目录 使用 slot 基础用法 具名插槽 作用域插槽 slot 实现 总结 在Vue中,我们使用组件来组织页面和组织代码,类似于搭积木,每一个组件都是一个积木,使用一些相同或者不同组件就能搭建出我们想要的页面. slot(插槽)是组件功能的重要组成部分,插槽必须用于组件才有意义. 它为组件提供了对外的接口,允许从组件外部传递内容,并将这部分内容放置到指定的位置. 使用 slot 当一个组件可能被使用至少两次并且两次使用内容(这里指组件视图的组成)不同时,插槽才有存在的必要.注意: 本文的代码都是

  • Vue中的slot使用插槽分发内容的方法

    本文题材来自:https://cn.vuejs.org/v2/guide/components.html#%E4%BD%BF%E7%94%A8%E6%8F%92%E6%A7%BD%E5%88%86%E5%8F%91%E5%86%85%E5%AE%B9 <slot></slot>标签,简单来说就是占位符,它会帮你占好位置,等你需要的时候直接将html传入,它会帮你显示出来. 也有人说:props可以将数据从父组件传入子组件,slot可以将html从父组件传入子组件.那么如何实现呢?

  • 详解Vue中使用插槽(slot)、聚类插槽

    一.基本的插槽 这里总结两点 如果不在子组件中使用插槽(slot),那么在子组件中写任何代码都是无效的的,不会显示 (插槽默认值)如果子组件中没有插入任何代码的话就会显示组件插槽中的内容 slot 代表父组件往子组件中 插入的标签 这里就代表组件子组件中的 <p>Dell</p> <child> <p>Dell</p> </child> 这里如果是这样的 <child> </child> 就会显示 <sl

随机推荐