Vue框架插槽(第八课)

news2025/7/20 12:54:26

案例 组件信息的通信

自己思考一下 答案在本文章的后面

 

插槽 v-slot

  1. 这个时候我们就可以来定义插槽slot: 插槽的使用过程其实是抽取共性、预留不同;
  2. 我们会将共同的元素、内容依然在组件内进行封装;
  3. 同时会将不同的元素使用slot作为占位,让外部决定到底显示什么样的元素;
  4. 如何使用slot呢?
  5. Vue中将 <slot> 元素作为承载分发内容的出口;
  6. 在封装组件中,使用特殊的元素<slot>就可以为封装组件开启一个插槽;
  7. 该插槽插入什么内容取决于父组件如何使用;

插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签。简单理解就是子组件中留下个“坑”,父组件可以使用指定内容来补“坑”.

页面控制平台

 案例一

这个案例带你去理解啥是插槽

 主页面

   <template #bbb>
        name=BBB请输入密码:<input type="password" name="" id="" />
      </template>

<template>
  <div class="App">
    <headerTab>
      <!-- v-solt 插槽 名称 -->
      <template v-slot:aaa>
        <a href="name=AAA返回按钮<">name=AAA返回按钮</a>
      </template>

      <template #bbb>
        name=BBB请输入密码:<input type="password" name="" id="" />
      </template>
      <template #ccc>
        name=CCC:<input type="Number">
      </template>

      <template #ddd>name=DDD <input type="checkbox" name="" id="" />男 </template>

      <template #eee> name=EEE<input type="checkbox" name="" id="" />女</template>

      <template #[msg]><button>name=fff你好呀</button> </template>
    </headerTab>

    <headerTab>
      <template #[msg]>
        <button>返回按钮</button>
      </template>
      <template v-slot:aaa>
        <a href="#">返回按钮</a>
      </template>
      <template #bbb>
        请输入密码:<input type="password" name="" id="" />
      </template>
      <template #ccc>
        点击按钮:<button>{{ msg }}</button>
      </template>
      <template #ddd> <input type="checkbox" name="" id="" />男 </template>
      <template #eee> <input type="checkbox" name="" id="" />女</template>
    </headerTab>

    <button @click="msg = 'left'">left</button>
    <button @click="msg = 'center'">center</button>
    <button @click="msg = 'right'">right</button>
    <button @click="msg = 'left'">left</button>
    <button @click="msg = 'aaa'">aaa</button>
    <button @click="msg = 'bbb'">bbb</button>
    <button @click="msg = 'ccc'">ccc</button>
    <button @click="msg = 'ddd'">ddd</button>
    <p>返回</p>
    <hr />
    <headerTab :propList="propList">
      <template v-slot="slotProps">
        <div>{{ slotProps.item + "&nbsp;&nbsp;&nbsp;" + slotProps.index }}</div>
      </template>
    </headerTab>
    <hr />
    <!-- <headerTab :propList="propList" v-slot="slotProps"> -->
    <!-- <div>{{slotProps.item+'-'+slotProps.index}}</div> -->
    <!-- </headerTab> -->
  </div>
</template>

<script>
import headerTab from "./components/header-tab.vue";
export default {
  name: "App",
  components: {
    headerTab,
  },
  data() {
    return {
      msg: "我是登录按钮",
      propList: ["王者荣耀", "JavaScript", "Jquery", "Web", "EEEE"],
    };
  },
  methods: {},
};
</script>

<style scoped>
div{
  border-top: 2px solid red;
  background-color: rgb(124, 188, 225);
}
</style>

子组件

 <div class="content">
  <div class="item left">
    <slot name="aaa"> 请输入姓名:<input type="text" /> </slot>
  </div>
  <div class="item"><slot name="bbb">请输入邮箱地址:<input type="emil" /></slot></div>
  <div class="item"><slot name="ccc">我是a链接:<a href="#">我是a标签</a></slot></div>
  <div class="item center">
    <!-- 命令 -->
    <slot name="ddd">标题</slot>
  </div>
  <div class="item right">
    <slot name="eee">登录</slot>
  </div>

<template>
  <div class="content">
    <div class="item left">
      <slot name=" item lefts"> 请输入姓名:<input type="text" /> </slot>
    </div>
    <div class="item">请输入邮箱地址:<input type="emil" /></div>

    <div class="item">我是a链接:<a href="#">我是a标签</a></div>
    <div class="item center">
      <slot name="center">标题</slot>
    </div>
    <div class="item right">
      <slot name="right">登录</slot>
    </div>
  </div>

  <div class="content">
  <div class="item left">
    <slot name="aaa"> 请输入姓名:<input type="text" /> </slot>
  </div>
  <div class="item"><slot name="bbb">请输入邮箱地址:<input type="emil" /></slot></div>
  <div class="item"><slot name="ccc">我是a链接:<a href="#">我是a标签</a></slot></div>
  <div class="item center">
    <!-- 命令 -->
    <slot name="ddd">标题</slot>
  </div>
  <div class="item right">
    <slot name="eee">登录</slot>
  </div>


</div>

  <div>
  <div class="item" v-for="item,index in propList" :key="item">
  <slot :item="item" :index="index">登录信息</slot>
          
  <slot name="two">我是数据信息</slot>
  </div>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "left",
  props: ["propList"],
  data() {
    return {
      msg: "子组件的msg",
    };
  },
  methods: {},
};
</script>

<style scoped>
.content {
  display: flex;
  height: 50px;
  line-height: 50px;
  text-align: center;
  display: flex;
}
.item {
  margin-top: 3px;
  border-left: 2px solid red;
  flex: 1;
  background-color: rgb(166, 236, 167);
  color: rgb(0, 0, 0);
}
.left {
  background: rgb(171, 220, 223);
}
.center {
  background: rgb(50, 174, 28);
}
.right {
  background: rgb(10, 155, 213);
}
</style>

案例二  兄弟的组件直接转送值

 

定义对象的属性值

 

取出值展示在页面上

 

主页面

<template>
  <div class="app">
    <h3 style="color:red">Provide和Inject基本使用</h3>
    <home></home>
    <hr>
    <content></content>
  </div>
</template>


<script>
import content from "./components/content.vue";
import home from "./components/home.vue";

export default {
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },

  components: {
    // eslint-disable-next-line vue/no-unused-components
    content,
    // eslint-disable-next-line vue/no-unused-components
    home,
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
};
</script>



<style scoped>
</style>

子组件1

<template>
  <div class="content">
    <home></home>
  </div>
</template>


<script>
import home from "../components/home.vue";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "content",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  // 定义对象
  provide: {
    name: "李四",
    age: 20,
    height: 123,
    weight: 78,
    email: "2678903458@qq.com",
    qq: "2386754567",
    weixing: "12389999933",
  },
  // 函数的写法

  methods: {
    btnclick() {
      console.log("为难忘");
    
    },
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    home,
  },
};
</script>



<style scoped>
</style>

 // 定义对象
  provide: {
    name: "李四",
    age: 20,
    height: 123,
    weight: 78,
    email: "2678903458@qq.com",
    qq: "2386754567",
    weixing: "12389999933",
  },

子组件2

  //   取值
  inject: ["name", "age", "height", "weight", "email", "qq", "weixing"],

<template>
  <h6>
    在home.vue组件中和content组件中利用 provide定义对象 利用 inject取出对象
  </h6>
  <div class="home">
    <content></content>
    <table>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>身高</th>
        <th>体重</th>
        <th>邮箱</th>
        <th>qq</th>
        <th>微信</th>
      </tr>
      <tr>
        <td>
          <span>{{ name }}</span>
        </td>
        <td>
          <span>{{ age }}</span>
        </td>
        <td>
          <span>{{ height }}</span>
        </td>
        <td>
          <span>{{ weight }}</span>
        </td>
        <td>
          <span>{{ email }}</span>
        </td>
        <td>
          <span>{{ qq }}</span>
        </td>
        <td>
          <span>{{ weixing }}</span>
        </td>
      </tr>
    </table>
  </div>
</template>
<script>
import content from '../components/content.vue'
// home 与content组件为兄弟元素
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "home",
  data() {
    return {
      message: "Hello world vue cli",
    };
    
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    content
  },
  //   取值
  inject: ["name", "age", "height", "weight", "email", "qq", "weixing"],

  methods: {
    btnclick() {
      console.log("为难忘");
    },
    
  },
};
</script>



<style scoped>
.home {
  background-color: rgb(214, 248, 177);
  display: flex;
}
.home span {
  font-size: 20px;
  background-color: rgb(236, 253, 239);
  color: red;
  flex: 1;
}
table {
  width: 100%;
  background-color: rgb(176, 230, 232);
  color: rgb(255, 0, 0);
  font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}

td {
  width: 18%;
  border-radius: 10px;
}
td {
  text-align: center;
  line-height: 60px;
  height: 60px;
  border: 3px solid green;
}
</style>

组件通信答案

<!-- eslint-disable vue/require-v-for-key -->
<template>
  <div class="app">
    <div class="big_father">
      <span
        v-for="(item, index) in arrays"
        :key="index.id + ''"
        class=".big_father"
        :class="{ active: isshow === index }"
        @click="btnclick(index)"
        >{{ item }} {{ index }}</span
      >
    </div>




    <div>
    <span
   v-for="(item, index) in infor"
   :key="index.id + ''"
   class=".big_father"
   :class="{ active: isshow === index }"
   @click="btnclick1(ko)">{{ item }} {{ index }}</span
 >
    </div>

    <yf v-show="isshow === 0"></yf>
    <kz v-show="isshow === 1"></kz>
    <xz v-show="isshow === 2"></xz>
    <sy v-show="isshow === 3"></sy>
    <wt v-show="isshow === 4"></wt>
  </div>
</template>


<script>
// 导入文件信息
import yf from "./components/yf.vue";
import xz from "./components/xz.vue";
import kz from "./components/kz.vue";
import sy from "./components/sy.vue";
import wt from './components/wt.vue';

export default {
  components: {
    // eslint-disable-next-line vue/no-unused-components
    yf,
    // eslint-disable-next-line vue/no-unused-components
    xz,
    // eslint-disable-next-line vue/no-unused-components
    kz,
    // eslint-disable-next-line vue/no-unused-components
    sy,
    wt
  },
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",
      isshow: 0,
      arrays: ["衣服", "鞋子", "裤子", "上衣", "外套"],
      infor: ["衣服页面", "鞋子页面", "裤子页面", "上衣页面", "外套页面"],
      index:0,
      ko:true
      
    };
  },
  methods: {
    btnclick(flag) {
      this.isshow = flag;
    },
    btnclick1(ko){
      this.ko= !ko
    }


  },
};
</script>



<style scoped>
.active {
  color: red;
  background-color: azure;
  border: 2px solid lightseagreen;
  border-bottom: 8px solid red;
  border-radius: 12px;
}
.big_father {
  display: flex;
}

.big_father div {
  text-align: center;
  height: 60px;
  line-height: 60px;
  flex: 1;
  color: white;
  background-color: rgb(28, 125, 147);
}

.big_father div span {
  text-align: center;
  height: 60px;
  line-height: 60px;
  width: 100px;
  flex: 1;
  color: white;
  background-color: rgb(116, 214, 236);
}
button {
  height: 60px;
  width: 100px;
  line-height: 60px;
  margin-left: 100px;
}
span {
  text-align: center;
  line-height: 60px;
  margin-left: 10px;
  display: inline-block;
  width: 17%;
  height: 60px;
  font-size: 20px;
  border-radius: 20px;
  background: rgb(121, 224, 235);
  border-right: 2px solid red;
}
</style>

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CTC 技术介绍概述——啃论文系列

CTC 技术介绍概述——啃论文系列 文章目录CTC 技术介绍概述——啃论文系列自我介绍摘要前言知识导图1. 定义2. 诞生背景2.1 频谱紧张例子&#xff0c;wifi的5GHz2.2 通信干扰——CTI2.3 管理困难2.4 异构通信传统实现——网关桥接2.5 CTC——异构直接通信3. 包级CTC3.1 基于RSS…

个人设计web前端大作业 基于html5制作美食菜谱网页设计作业代码

&#x1f380; 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…

基于PHP+MySQL长途客用汽车票订票系统的设计与实现

随着时代的变迁汽车已经成为了人们日常生活中不可或缺的一部分,虽然很多人已经拥有了私家车,但是很多时候因为离家较远等原因,很多时候人们还是会通过客用汽车来进行一些出行,但是通常情况下客用车票都需要到客用站进行购买,这极其的不便利。 为了能够让用户足不出户就可以进行…

拿稳这24道JVM面试题,要价30k都有底气

1.什么是JVM? JVM 的全称是 「Java Virtual Machine」&#xff0c;也就是我们耳熟能详的 Java 虚拟机。它能识别 .class后缀的文件&#xff0c;并且能够解析它的指令&#xff0c;最终调用操作系统上的函数&#xff0c;完成我们想要的操作。 C开发出来的程序&#xff0c;编译…

SpringBoot SpringBoot 开发实用篇 4 数据层解决方案 4.10 MongoDB 基础操作

SpringBoot 【黑马程序员SpringBoot2全套视频教程&#xff0c;springboot零基础到项目实战&#xff08;spring boot2完整版&#xff09;】 SpringBoot 开发实用篇 文章目录SpringBootSpringBoot 开发实用篇4 数据层解决方案4.10 MongoDB 基础操作4.10.1 MongoDB 基础操作4.10…

(3)paddle---近视眼睛分类的例子

1主要参考 &#xff08;0&#xff09;本教程和以下教程不够详细&#xff0c;还是推荐下面这个大佬的blog看一下 计算机视觉——眼疾图片识别&#xff08;数据集iChallenge-PM&#xff09;_「已注销」的博客-CSDN博客 &#xff08;1&#xff09;blibli视频 252-06_预测病理性…

Vite2 + Vue3 + TypeScript + Pinia 搭建一套企业级的开发脚手架

Vue2 与 Vue3 的区别 Vue3由于完全由TS进行重写&#xff0c;在应用中对类型判断的定义和使用有很强的表现。同一对象的多个键返回值必须通过定义对应的接口&#xff08;interface&#xff09;来进行类型定义。要不然在 ESLint 时都会报错。Vue2 与 Vue3 最大的区别: Vue2 使用…

jquery把页面<table>里的内容导出为后缀名为.xlsx的excel

1、问题描述 之前是用Blob把页面的<table>导出成.xls的Excel文件&#xff1a;Blob把html导出为excel文件_金斗潼关的博客-CSDN博客 不过由于微软的Excel对.xls扩展名的文件支持不是很好&#xff0c;打开会报一个警告 所以用户反馈说是不想弹出这个警告&#xff0c;要求…

【深度学习入门 2022 最新版】第一课 深度学习简介

【深度学习入门 2022 最新版】第一课 深度学习简介概述深度学习 vs 机器学习机器学习是什么深度学习是什么机器学习和深度学习的区别神经网络机器学习实现二分类神经网络实现二分类TensorFlowPyTorch神经网络的原理张量张量最小值 (补充)张量最大值 (补充)前向传播损失计算反向…

手撕AVL树

目录 一、概念 二、 结点的定义 2.1 键值对pair 2.2 定义细节 三、 AVL树的插入操作 3.1 平衡因子调整规则 3.2 旋转规则 3.2.1 新节点插入较高左子树的左侧 — 左左:右单旋 3.2.2 新节点插入较高右子树的右侧 — 右右:左单旋 3.2.3 新节点插入较高左子树的右侧 — …

论文管理系统(准备工作)

目录 一、项目需求响应图 二、准备工作 2.1创建一个Spring Initializr项目 2.2后端架构 2.2.1 controller层 2.2.2 entity层 2.2.3 interceptors层 2.2.4 mapper层 2.2.5 serivice层 2.2.6 main运行 2.2.7 mappers文件 2.3配置 application.yml文件 2.4加入依赖 一、项…

Postman如何携带token——Bearer Token和Headers

目录一、使用场景二、设置Bearer Token1.设置你的环境变量2.项目集合设置认证方式及环境变量3.登录接口的脚本三、通过脚本设置Headers1.登录请求设置环境变量2.设置集合的发送请求脚本一、使用场景 现在许多项目都使用jwt来实现用户登录和数据权限&#xff0c;校验过用户的用…

C++ Builder XE关于AdvStringGrid对EXCEL母表快速分表,并批量插入sheet子表简单操作

如何快速将ECXEL母表快速批量生成多个子表分表&#xff0c;并且在表中插入sheet子表的简单操作&#xff1a; //AdvStringGrid2->SaveToXLS(filename,false);//生成新EXCEL表格 //AdvStringGrid2->SaveToXLSSheet(filename,Fname);//插入sheet子表 //-----------------…

一篇五分生信临床模型预测文章代码复现——Figure 4-6 临床模型构建(五)

之前讲过临床模型预测的专栏,但那只是基础版本,下面我们以自噬相关基因为例子,模仿一篇五分文章,将图和代码复现出来,学会本专栏课程,可以具备发一篇五分左右文章的水平: 本专栏目录如下: Figure 1:差异表达基因及预后基因筛选(图片仅供参考) Figure 2. 生存分析,…

XSS-labs靶场实战(五)——第12-14关

今天继续给大家介绍渗透测试相关知识&#xff0c;本文主要内容是XSS-labs靶场实战第12-14关。 免责声明&#xff1a; 本文所介绍的内容仅做学习交流使用&#xff0c;严禁利用文中技术进行非法行为&#xff0c;否则造成一切严重后果自负&#xff01; 再次强调&#xff1a;严禁对…

认识和使用容器

目录 前言 一、容器化背后的发展历史和概念 1.容器的抽象 容器的比喻 2.计算机领域的容器 容器是一种标准化的软件单元 二、容器和微服务架构 1.容器的作用 快速搭建开发环境 将运行环境和配置放在代码中并部署 使用docker-compose来模拟生产环境 使用docker镜像进…

在JVM 中进程与线程关系、介绍线程:程序计数器、本地方法栈、虚拟机栈

首先,我们要了解进程和线程的基本概念 进程 process 一个在内存中运行的应用程序。每个进程都有自己独立的一块内存空间,一个进程可以有多个线程,比如在Windows系统中,一个运行的*.exe应用程序就是一个进程。 线程 thread 进程中的一个执行任务(控制单元),负责当前进…

vue-element-admin依赖报错npm ERR! code 128 npm ERR! An unknown git error occurred

解决vue-element-admin安装报错 npm ERR code 128 npm ERR An unknown git error occurred npm 报错截图&#xff1a; 参考地址 先试一下&#xff1a;控制台输入&#xff1a; git config --global http.sslverify “false” git config --global url.“https://”.insteadOf …

几行代码实现用Python输出表情包

近几日在搞邮件自动发送&#xff0c;发现python原来可以发小表情&#xff01;而且操作很容易&#xff0c;但是发现现在的博文介绍的不是很全面&#xff08;或者我没搜出来……&#xff09;&#xff0c;因此在此补充一二。 1. python输出的表情样子 图里的表情包当然才是冰山一角…

【案例 5-3】 模拟用户登录

【案例介绍】 1.任务描述 在使用一些 APP 时&#xff0c;通常都需要填写用户名和密码。用户名和密码输入都正确才会登录成功&#xff0c;否则会提示用户名或密码错误。 本例要求编写一个程序&#xff0c;模拟用户登录。程序要求如下&#xff1a; &#xff08;1&#xff09; 用…