前端基础(props emit slot 父子组件间通信)

news/2024/7/10 2:30:29 标签: 前端, 开发语言, vue

前言:如何实现组件的灵活使用,今天学习组件封装用到的props、slot和emit。

目录

props

子组件

父组件

示例代码

slot

示例代码

作用域插槽

emit

示例代码


props

需要实现在其他组件中使用同一个子组件。

子组件

子组件(所谓子组件,就是封装好的组件,供其他组件使用)

子组件定义了sonName

<div>我是MRJJ_9{{sonName}}</div>

defineProps(['sonName'])

或 const props=defineProps(['sonName'])

props是只读的,尽量不要去修改

定义多个

const props=defineProps(['sonName1','sonName2'])

但通常使用数组定义

const props = defineProps({
  sonName1: Object,
  sonName: Number,})

父组件

父组件(所谓父组件,就是引用封装好的其他子组件)

<Mrjj-Counter :sonName="sonName"></Mrjj-Counter>

let sonName=ref("引用子组件")

示例代码

子组件设置

<template>
  <div>我是MRJJ_9的第一个属性,类型为字符串,内容是:{{ sonName1 }},第二个属性,类型是数字,数值为:{{ sonName2 }}</div>
</template>
<script setup>
const props = defineProps({
  sonName1: String,
  sonName2: Number,
})
console.log("属性1",props.sonName1)
console.log("属性2",props.sonName2)
</script>
<style lang="scss" scoped>
</style>

父组件设置

<template>
  <mrjj-son :sonName1="sonName1" :sonName2="sonName2"></mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
</script>
<style lang="scss" scoped>
</style>

 

要注意不能去修改里面的值

slot

需要实现在其他组件中使用同一个组件(子组件),但组件样式的有所区别

这就需要用到插槽:slot,其作用是传参时可以带上HTML结构

子组件带上slot

{{ sonName }}<slot></slot>

父组件将需要传递的内容写到子组件标签里

<mrjj-son><strong>{{sonName }}</strong></mrjj-son>

具名插槽,给插槽命名

有多个值时

子组件加上name

父组件,用v-slot:插槽名(或#插槽名)

示例代码

子组件设置

<template>
  <div>
    {{ sonName1 }}
    <slot name="mrjj1"></slot>
    {{ sonName2 }}
    <slot name="mrjj2"></slot>
  </div>
</template>
<script setup>
const props = defineProps({
  sonName1: String,
  sonName2: Number
})
</script>
<style lang="scss" scoped>
</style>

父组件设置

<template>
  <mrjj-son>
    <template #mrjj1
      ><strong>{{ sonName1 }}</strong></template
    >
    <template #mrjj2
      ><i>{{ sonName2 }}</i></template
    >
  </mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
</script>
<style lang="scss" scoped>
</style>

效果展示

作用域插槽

子组件

<template>
  <div>
    {{ sonName3 }}
    <slot name="mrjj3" :times="count" :mrjj1="name"></slot>
  </div>
</template>
<script setup>
import { ref } from 'vue'

const props = defineProps({
  sonName3: String
})
let count = ref(0)
let name = ref('计数器')
</script>
<style lang="scss" scoped>
</style>

父组件 

<template #mrjj3="{ times }"

<template>
  <mrjj-son>
    <template #mrjj3="{ times }"
      ><i>{{ sonName3 }}</i>
      <Times :times="times"></Times>
    </template>
  </mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import Times from '@/components/Times.vue'
import { ref } from 'vue'
let sonName3 = ref('')
</script>
<style lang="scss" scoped>
</style>

引用的Time.vue文件

<template>
  <h1>显示Mrjj{{ times }}</h1>
</template>
<script setup>
defineProps(['times'])
</script>

emit

需求:增加一个关闭、打开的组件功能

用到emit,emit干了什么事情呢?在子组件中触发一个事件,在父组件中进行监听。

示例代码

子组件定义一个自定义事件

<template>
  <div>
    {{ sonName1 }}
    <slot name="mrjj1"></slot>
    {{ sonName2 }}
    <slot name="mrjj2"></slot>
    <button @click="closeSon">点我关闭</button>
  </div>
</template>


<script setup>
const props = defineProps({
  sonName1: String,
  sonName2: Number
})
const emit = defineEmits(['closeMrjj'])

function closeSon() {
  console.log('关闭按钮被点击了!')
  emit('closeMrjj')
}
</script>
<style lang="scss" scoped>
</style>

父组件绑定事件

<template>
  <mrjj-son @closeMrjj="closeMrjj" v-if="isClose">
    <template #mrjj1
      ><strong>{{ sonName1 }}</strong></template
    >
    <template #mrjj2
      ><i>{{ sonName2 }}</i></template
    >
  </mrjj-son>
  <button v-else @click="($event) => (isClose = true)">点我打开</button>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
let isClose = ref(false)
function closeMrjj() {
  isClose.value = false
}
</script>
<style lang="scss" scoped>
</style>

效果展示

点我关闭按钮,点击后,调用了closeSon函数,可以看到console输出的信息。

点击展开后,也可以展示出内容。


http://www.niftyadmin.cn/n/4965455.html

相关文章

VMware虚拟机连不上网络

固定ip地址 进入网络配置文件 cd /etc/sysconfig/network-scripts 打开文件 vi ifcfg-ens33 编辑 BOOTPROTO设置为static&#xff0c;有3个值&#xff08;decp、none、static&#xff09; BOOTPROTO"static" 打开网络 ONBOOT"yes" 固定ip IPADDR1…

每日一学——认识路由器

路由器是一种网络设备&#xff0c;它用于在计算机网络中转发数据包。它能够在不同的网络之间选择最佳路径&#xff0c;将数据从源地址传输到目的地址。 路由器工作原理&#xff1a; 路由表&#xff1a;路由器内部有一个路由表&#xff0c;它记录了网络的连接关系和路径。路由器…

【React源码实现】元素渲染的实现原理

前言 本文将结合React的设计思想来实现元素的渲染&#xff0c;即通过JSX语法的方式是如何创建为真实dom渲染到页面上&#xff0c;本文基本不涉及React的源码&#xff0c;但与React的实现思路是一致的&#xff0c;所以非常适合小白学习&#xff0c;建议跟着步骤敲代码&#xff…

Redis从入门到精通!!

❤ 作者主页&#xff1a;李奕赫揍小邰的博客 ❀ 个人介绍&#xff1a;大家好&#xff0c;我是李奕赫&#xff01;(&#xffe3;▽&#xffe3;)~* &#x1f34a; 记得点赞、收藏、评论⭐️⭐️⭐️ &#x1f4e3; 认真学习!!!&#x1f389;&#x1f389; 文章目录 Redis从入门到…

死锁的典型情况、产生的必要条件和解决方案

前言 死锁&#xff1a;多个线程同时被阻塞&#xff0c;他们中的一个或全部都在等待某个资源被释放。由于线程被无限期地阻塞&#xff0c;因此程序不可能正常终止。 目录 前言 一、死锁的三种典型情况 &#xff08;一&#xff09;一个线程一把锁 &#xff08;二&#xff09;…

mysql 默认的4个数据库 介绍

mysql 存储MySQL的用户账号和权限信息&#xff0c;一些存储过程、事件的定义信息 一些运行过程中产生的日志信息&#xff0c;一些帮助信息以及时区信息等 information_schema 存储Mysql服务器 维护的所有其它数据库的信息&#xff0c;比如有哪些表、哪些视图、哪些触发器、哪…

RJ11接口浪涌防护推荐半导体放电管和ESD二极管

在POS机、ATM系统和安防系统中&#xff0c;经常需要采用RJ11接口用于拨号上网、设备间或者与上位机之间的数据通信。RJ11接口浪涌保护与RS-485、232等接口不同&#xff0c;由于RJ11的Tip和Ring信号在线振铃电压的存在&#xff0c;而该振铃信号的最大电压值可能超过100V&#xf…

猜数游戏-Rust版

cargo new guessing_game 创建项目 输入任意内容&#xff0c;并打印出来 main.rs: use std::io; // 像String这些类型都在预先导入的prelude里&#xff0c;如果要使用的不在prelude里&#xff0c;则需要显式导入fn main() { println!("猜数"); println!("…