【vue】vue中的插槽以及使用方法

 插槽

普通插槽

1、在父组件中直接调用子组件的标签,是可以渲染出子组件的内容;如果在子组件标签中添加了内容,父组件就渲染不出来了;

ParentComponent.vue:

<template>
  <div>
    <h1>Parent Component</h1>
    <child-component>
      <p>This is custom content inside the child component.</p>
    </child-component>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  }
};
</script>

无名插槽(默认插槽)

ChildComponent.vue:

<!--    第一种方式-->
<template>
  <div>
    <h2>Child Component</h2>
    <slot></slot>
    <p>This is content from the child component.</p>
  </div>
</template>

<script>
export default {
  name: 'ChildComponent'
};
</script>


2、如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容;

<!--    第二种方式-->
<template>
  <div>
    <h2>Child Component</h2>
    <slot><p>我们一起学猫叫</p></slot>
    <p>This is content from the child component.</p>
  </div>
</template>

<script>
export default {
  name: 'ChildComponent'
};
</script>

在上述示例中,ChildComponent 组件使用了一个无名插槽(默认插槽)。在 ParentComponent 中,通过将内容包裹在 <child-component> 标签中,该内容就会被插入到 ChildComponent 的插槽中。 

index.js

以父组件为loginnew,子组件为hello-world为例;

<!--父组件loginnew.vue->>
<hello-world></hello-world>
<hello-world>这是个hello-world插槽位</hello-world>
<!--如果想要渲染出父组件中调用子组件标签中的内容,就要在子组件中添加插槽-->
<!--子组件hello-world.vue文件-->
<!--如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容-->
<slot><p>hello-world:我们一起学猫叫</p></slot>

 具名/命名插槽

是 Vue.js 组件中的一种高级插槽技术,允许您在组件中定义多个具有名称的插槽,以便更精细地控制不同部分的内容插入位置。通过使用具名插槽,您可以在父组件中传递不同的内容到不同的插槽位置,从而实现更灵活和定制化的布局和组件复用。

以下是一个使用具名插槽的示例:

ChildComponent.vue:

<template>
  <div>
    <h2>Child Component</h2>
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>

<script>
export default {
  name: 'ChildComponent'
};
</script>

ParentComponent.vue:

<template>
  <div>
    <h1>Parent Component</h1>
    <child-component>
      <template v-slot:header>
        <p>This is the header content.</p>
      </template>
      <p>This is the main content.</p>
      <template v-slot:footer>
        <p>This is the footer content.</p>
      </template>
    </child-component>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  }
};
</script>

在上述示例中,ChildComponent 组件定义了三个插槽,分别是默认插槽以及具名插槽 headerfooter。在 ParentComponent 中,使用 <template> 元素配合 v-slot 指令来填充具名插槽的内容。

注意,Vue 2.6.0 及以上版本引入了新的缩写语法,将 v-slot:header 缩写为 #header,这样可以更简洁地使用具名插槽。

示例中的 ParentComponent 会渲染成如下内容:

<div>
  <h1>Parent Component</h1>
  <div>
    <h2>Child Component</h2>
    <p>This is the header content.</p>
    <p>This is the main content.</p>
    <p>This is the footer content.</p>
  </div>
</div>

通过使用具名插槽,您可以在不同的插槽位置插入不同的内容,从而实现更灵活和可配置的组件。具名插槽使得您的组件能够更好地适应各种不同的使用场景。 

父组件loginNew.vue:

<template>
  <div>
    <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="70px" class="demo-ruleForm"
             :label-position="labelPosition">
      <el-form-item label="用户名" prop="username">
        <el-input type="username" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!--  如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容-->
    <!--  <hello-world>这是个hello-world插槽位</hello-world>-->
    <!--  如果父组件调用的子组件标签中和子组件中的插槽中都有文本内容,那么父组件中的会覆盖子组件插槽中的内容-->
    <!--  <hello-world></hello-world>-->
    <hello-world>
      <!--    方法二  命名插槽-->
      <!--    在vue2.6之前版本-->
      <p slot="part1">一起喵喵喵</p>
      <!--    在vue2.6之后版本-->
      <template v-slot:part2>
        <p>在你面前撒个娇</p>
      </template>
      <!--       v-slot:可以简写成"#" -->
      <template #part3>
        <p>还是喵喵喵喵</p>
      </template>

      <!--        插槽作用域:父组件调取子组件的插槽内部要获取子组件的属性-->
      <!--        2.6 之前-->
      <p slot="part4" slot-scope="scope">
        {{ scope.user }}我得心脏砰砰跳
      </p>
      <template slot="part5" slot-scope="scope">
        <p>{{ scope.user }}我得心脏砰砰跳aaaaaa</p>
      </template>
      <!--        2.6 之后-->
      <template v-slot:part6="scope">
        <p>{{scope.user}}都是你的味道</p>
      </template>

      <template v-slot:part7="{user}">
        <p>{{user}}都是你的味道</p>
      </template>
    </hello-world>

  </div>
</template>

<script>
export default {
  name: "loginNew",
  data() {
    return {
      username: "daxiao",
      password: "123456",
      labelPosition: "right",
      ruleForm: {
        username: "111",
        password: "222",
      }
    }
  },
}
</script>

<style scoped>
.el-form {
  width: 350px;
  margin: 50px auto;
}
</style>

子组件HelloWorld.vue:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h>{{ msg1 }}</h>
    <p>这是一个hello-world页面</p>
    <div>
      <el-image
          style="width: 300px; height: 200px"
          :src="url"
          fit="cover"></el-image>
    </div>
    <!--    第一种方式-->
    <!--    <slot></slot>-->
    <!--    第二种方式-->
    <slot><p>我们一起学猫叫</p></slot>
    <!--    第三种方式 命名插槽-->
    <slot name="part1"></slot>

    <slot name="part2"></slot>

    <slot name="part3"></slot>
    <!--    插槽作用域-->
    <slot name="part4" :user="username"></slot>

    <slot name="part5" user="六啊"></slot>

    <slot name="part6" user="七啊"></slot>

    <slot name="part7" user="八啊"></slot>

    <!--    <slot ></slot>-->
  </div>
</template>

<script>
// import axios from 'axios';
import {dogs} from '../api/api'

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data() {
    return {
      url: '',
      username: "木子"
    }
  },
  mounted() {
    //方法一:不推荐
    // axios.get('https://dog.ceo/api/breeds/image/random')
    //     //如果请求成功,就会执行.then回调函数
    //     .then(function (response) {
    //       console.log('data:',response.data)
    //       console.log('response:',response)
    //       //此时的this指的是当前函数的应用
    //       this.url=response.data.message
    //     })
    //     //如果请求失败,就会执行.catch回调函数
    //     .catch(function (err) {
    //       console.log(err)
    //     });
    // axios.get('https://dog.ceo/api/breeds/image/random')
    //     //如果请求成功,就会执行.then回调函数
    //     //方法二:使用箭头函数
    //     .then(response => {
    //       console.log('data:', response.data)
    //       console.log('response:', response)
    //       //此时的this指的是当前函数的应用
    //       this.url = response.data.message
    //     })
    //     //如果请求失败,就会执行.catch回调函数
    //     .catch(function (err) {
    //       console.log(err)
    //     });
    dogs()
        //如果请求成功,就会执行.then回调函数
        //方法二:使用箭头函数
        .then(response => {
          console.log('data:', response.data)
          console.log('response:', response)
          //此时的this指的是当前函数的应用
          this.url = response.data.message
        })
        //如果请求失败,就会执行.catch回调函数
        .catch(function (err) {
          console.log(err)
        });
  }
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

作用域插槽

是 Vue.js 组件中一种高级插槽技术,它允许父组件向子组件传递数据,并在子组件中根据这些数据自定义渲染逻辑。作用域插槽允许子组件对传递的数据进行更灵活的处理和展示,从而实现更高级的定制。

作用域插槽适用于以下情况:

  • 当父组件需要向子组件传递数据,以在子组件内部进行渲染和处理。
  • 当子组件需要在不同的上下文中使用传递的数据,例如在列表渲染或嵌套组件中。

以下是一个使用作用域插槽的示例:

List.vue:

<template>
  <div>
    <ul>
      <li v-for="(item, index) in items" :key="item.id">
        <slot :item="item" :index="index"></slot>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'List',
  props: {
    items: Array
  }
};
</script>

ParentComponent.vue:

<template>
  <div>
    <h1>Parent Component</h1>
    <list :items="dataItems">
      <template v-slot="slotProps">
        <p>Item {{ slotProps.index }}: {{ slotProps.item.name }}</p>
      </template>
    </list>
  </div>
</template>

<script>
import List from './List.vue';

export default {
  name: 'ParentComponent',
  components: {
    List
  },
  data() {
    return {
      dataItems: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' }
      ]
    };
  }
};
</script>

在上述示例中,List 组件使用作用域插槽将每个列表项的数据和索引传递给插槽内容。在 ParentComponent 中,通过 <template> 元素使用 v-slot 缩写来定义作用域插槽,并在插槽内部使用传递的数据进行渲染。

作用域插槽的特点是,它将子组件内部的渲染逻辑交由父组件控制,子组件只需要关心数据的展示。这样可以实现更大程度的组件复用和定制。

 作用域插槽是 Vue.js 中非常强大和有用的特性,能够使您的组件更加灵活和高效

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/74812.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

常见分辨率时序信息

分辨率列表 分辨率一:640x480(逐行) 分辨率二:800x600(逐行) 分辨率三:1024x768(逐行) 分辨率四:大名鼎鼎720P(逐行) 注:选择720P@30帧的,需拉长HOR TOTAL TIME 分辨率五:1280x800(逐行) 分辨率六:1280x960(逐行

AWS——04篇(AWS之Amazon S3(云中可扩展存储)-02——EC2访问S3存储桶)

AWS——04篇&#xff08;AWS之Amazon S3&#xff08;云中可扩展存储&#xff09;-02——EC2访问S3存储桶&#xff09; 1. 前言2. 创建EC2实例 S3存储桶3. 创建IAM角色4. 修改EC2的IAM 角色5. 连接EC2查看效果5.1 连接EC25.2 简单测试5.2.1 查看桶内存储情况5.2.2 复制本地文件…

DIP: Spectral Bias of DIP 频谱偏置解释DIP

On Measuring and Controlling the Spectral Bias of the Deep Image Prior 文章目录 On Measuring and Controlling the Spectral Bias of the Deep Image Prior1. 方法原理1.1 动机1.2 相关概念1.3 方法原理频带一致度量与网络退化谱偏移和网络结构的关系Lipschitz-controlle…

GrapeCity Documents for PDF (GcPdf) 6.2 Crack

GrapeCity PDF 文档 (GcPdf) 改进了对由 GcPdf 以外的软件生成的现有 PDF 文档的处理 在新的 v6.2 版本中&#xff0c;GcPdf 增强了 PDF 文档的加载和保存&#xff0c;并提供以下优势&#xff1a; GcPdf 现在可以加载和保存可能不严格符合 PDF 规范的 PDF 文档。GcPdf 现在将…

Stable Diffusion + Deform制作指南

1.安装sd以及deform插件,更新后记得重启 需要安装ffmpeg https://ffmpeg.org/download.html 选择对应版本然后安装 如果是windows需要解压后将ffmpeg的bin目录配置在电脑的环境变量里面。 2.准备一张初始开始图片 3.填写参数,这里面参数要注意,宽高一定是32的倍数。如果填写…

【图像分类】理论篇 (4)图像增强opencv实现

随机旋转 随机旋转是一种图像增强技术&#xff0c;它通过将图像以随机角度进行旋转来增加数据的多样性&#xff0c;从而帮助改善模型的鲁棒性和泛化能力。这在训练深度学习模型时尤其有用&#xff0c;可以使模型更好地适应各种角度的输入。 原图像&#xff1a; 旋转后的图像&…

恒运资本:CPO概念发力走高,兆龙互联涨超10%,华是科技再创新高

CPO概念15日盘中发力走高&#xff0c;截至发稿&#xff0c;华是科技涨超15%再创新高&#xff0c;兆龙互联涨逾11%&#xff0c;中贝通讯涨停&#xff0c;永鼎股份、太辰光涨超5%&#xff0c;天孚通讯涨逾4%。 消息面上&#xff0c;光通讯闻名咨询机构LightCounting近日发布的202…

【LeetCode】《LeetCode 101》第十一章:妙用数据结构

文章目录 11.1 C STL11.2 数组448. 找到所有数组中消失的数字&#xff08;简单&#xff09;48. 旋转图像&#xff08;中等&#xff09;74. 搜索二维矩阵&#xff08;中等&#xff09;240. 搜索二维矩阵 II&#xff08;中等&#xff09;769. 最多能完成排序的块&#xff08;中等…

ChatGPT or BingChat

你相信我们对大模型也存在「迷信权威」吗&#xff1f; ChatGPT 的 GPT-4 名声在外&#xff0c;我们就不自觉地更相信它&#xff0c;优先使用它。但我用 ChatALL 比较 AI 大模型们这么久&#xff0c;得到的结论是&#xff1a; ChatGPT GPT-4 在大多数情况下确实是最强&#xf…

Element组件浅尝辄止4:Button组件

Button按钮组件&#xff1a;用途太广泛了&#xff0c;几乎参与到了日常开发中的方方面面 1.如何使用&#xff1f;How? //使用type、plain、round和circle属性来定义 Button 的样式。<el-row><el-button>默认按钮</el-button><el-button type"primar…

阿里云ACP知识点

前言&#xff1a;记录ACP错题 1、在创建阿里云ECS时&#xff0c;每台服务器必须要包含_______用来存储操作系统和核心配置。 系统盘&#xff08;不是实例&#xff0c;实例是一个虚拟的计算环境&#xff0c;由CPU、内存、系统盘和运行的操作系统组成&#xff1b;ESC实例作为云…

【RabbitMQ与SpringBoot集成测试收发消息】

【RabbitMQ与SpringBoot集成测试收发消息】 一、环境说明二、实验步骤三、小结 一、环境说明 安装环境&#xff1a;虚拟机VMWare Centos7.6 Maven3.6.3 JDK1.8RabbitMQ版本&#xff1a;rabbitmq-server-3.8.8-1.el7.noarch.rpm编程工具Idea 运行JDK为17 二、实验步骤 在Rab…

九州未来参与编制的开源领域3项团体标准获批发布

日前&#xff0c;中电标2023年第21号团体标准公告正式发布&#xff0c;其中由九州未来参与编制的3项开源领域团体标准正式获批发布&#xff0c;于2023年8月1日正式实施。 具体内容如下&#xff1a; 《T/CESA 1269-2023 信息技术 开源 术语与综述》&#xff0c;本文件界定了信息…

AWK +iptables+shell实战脚本案例

目录 一、在Centos下安装httpd 查看安装是否成功 重启httpd 查看80端口是否开放 在主机上查询 查看防火墙 在浏览器中查询主机IP地址 查看日志是否生成 二、AWK iptablesshell实战脚本案例 1、封堵扫描器 (1) 开始扫描器 特别注意&#xff1a;在Vim中尽量不要使用空格…

python_PyQt5运行股票研究python方法工具V1.2_增加折线图控件

承接【python_PyQt5运行股票研究python方法工具V1.1_增加表格展示控件】 地址&#xff1a;python_PyQt5运行股票研究python方法工具V1.1_增加表格展示控件_程序猿与金融与科技的博客-CSDN博客 目录 结果展示&#xff1a; 代码&#xff1a; 示例py文件代码&#xff08;低位股…

ubuntu 安装 cuda

ubuntu 安装 cuda 初环境与设备在官网找安装方式 本篇文章将介绍ubuntu 安装 CUDA Toolkit CUDA Toolkit 是由 NVIDIA&#xff08;英伟达&#xff09;公司开发的一个软件工具包&#xff0c;用于支持并优化 GPU&#xff08;图形处理器&#xff09;上的并行计算和高性能计算。它…

mysql的安装

首先双击mysql的安装包 双击安装包之后就会出现下面这种情况&#xff1b; 然后就会出现下面这个页面 选择developer default开发者模式&#xff0c;然后点击next 然后再点击next 再点击yes 点击excute&#xff0c;点击完之后需要稍等几分钟才能完成 上一步安装好之后点击n…

EthGlobal 巴黎站 Chainlink 获奖项目介绍

在 Web3 中&#xff0c;每一周都至关重要。项目的发布、版本的发布以及协议的更新以惊人的速度推出。开发者必须保持学习&#xff0c;随时了解最新的工具&#xff0c;并将所有他们所学的东西&#xff08;无论是旧的还是新的&#xff09;联系起来&#xff0c;以构建推动 Web3 技…

电脑垃圾清理怎么做?记住这5个方法!

“我的电脑用了快一年了吧&#xff01;现在感觉电脑里很多垃圾文件&#xff0c;但又害怕在删除这些垃圾文件时会将一些重要的文件一起删除掉。所以不敢对电脑进行清理。想问下大家平常有没有好用方法去清理电脑呀&#xff1f;感谢&#xff01;” 随着电脑使用时间的推移&#x…

AD23使用笔记

1. 如何修改原理图的页面 2. 原理图DRC&#xff1a;快捷键T D ; 或者&#xff1a;菜单→工程→validate pcb project,,,,,,,,, Altium Designer原理图错误编译检查_ad原理图如何编译和查错_y惘然__的博客-CSDN博客 3.
最新文章