当前位置: 首页 > article >正文

vue3组件通信--props

目录

  • 1.父传子
  • 2.子传父

最近在做项目的过程中发现,props父子通信忘的差不多了。下面写个笔记复习一下。

1.父传子

父组件(FatherComponent.vue):

<script setup>
import ChildComponent from "@/components/ChildComponent.vue"
import { ref } from "vue"

const fatherMoney = ref(1000)
</script>

<template>
  <div class="bg-blue h-75 w-100 ma-auto">
    <h1 class="text-center">我是父组件</h1>
    <ChildComponent :money="fatherMoney"></ChildComponent>
  </div>
</template>

我们可以在子组件标签上写:money="fatherMoney"。意思就是把父亲的响应式变量fatherMoney给子组件,子组件在组件内部要用money来接受这个变量。
子组件(ChildComponent.vue):

<script setup>
const props = defineProps(['money','updateMoney'])
</script>

<template>
  <div class="bg-purple h-50 w-75 ma-auto">
    <h1 class="text-center">我是子组件</h1>
    <h3>父亲给我的钱:{{money}}元</h3>
  </div>
</template>

子组件<h3>父亲给我的钱:{{money}}元</h3>这一块儿,我们可以用props.money来渲染这个数据,也可以省略props,直接写money

注意,用props来接受的数据是只读的,子组件不能再组件内部更改它。
比如,不能下面这样写,否则控制台会报错:

<script setup>
const props = defineProps(['money'])

const updateMoney = () => {
  props.money = 100
}
</script>

<template>
  <div class="bg-purple h-50 w-75 ma-auto">
    <h1 class="text-center">我是子组件</h1>
    <h3>父亲给我的钱:{{money}}元</h3>
    <v-btn @click="updateMoney" class="text-white bg-blue">修改父亲给我的钱</v-btn>
  </div>
</template>

在这里插入图片描述

2.子传父

子组件向父组件发送数据,父组件需要定义一个方法,用来接受子组件发送的数据:
父组件(FatherComponent.vue):

<script setup>
import ChildComponent from "@/components/ChildComponent.vue"
import { ref } from "vue"

const fatherMoney = ref(1000)

const childToy = ref('')
const getToy = (value)=>{
  childToy.value = value
}
</script>

<template>
  <div class="bg-blue h-75 w-100 ma-auto">
    <h1 class="text-center">我是父组件</h1>
    <h3>儿子给我的玩具:{{childToy}}</h3>
    <ChildComponent :money="fatherMoney" :sendToy="getToy"></ChildComponent>
  </div>
</template>

:sendToy="getToy"意思就是,父组件给子组件传递了一个方法getToy,子组件要用方法sendToy,给父亲发送数据。
子组件(ChildComponent.vue):

<script setup>
import {ref} from "vue"

const props = defineProps(['money','sendToy'])

const toy = ref('奥特曼')
</script>

<template>
  <div class="bg-purple h-50 w-75 ma-auto">
    <h1 class="text-center">我是子组件</h1>
    <h3>父亲给我的钱:{{money}}元</h3>
    <v-btn @click="sendToy(toy)" class="text-white bg-blue">把玩具给父亲</v-btn>
    <h3>儿子的玩具:{{toy}}</h3>
  </div>
</template>

在这里插入图片描述


http://www.kler.cn/news/367982.html

相关文章:

  • AAPL: Adding Attributes to Prompt Learning for Vision-Language Models
  • Java面试题——微服务篇
  • 【计网】从零开始认识IP协议 --- 认识网络层,认识IP报头结构
  • 《复旦学报(自然科学版)》
  • [计算机网络]第一周
  • Docker无法拉取镜像解决办法
  • 虚拟现实新纪元:VR/AR技术将如何改变娱乐与教育
  • 桥接模式,外界与主机通,与虚拟机不通
  • 提示词高级阶段学习day3.3如何写好结构化 Prompt ?
  • AndroidStudio Koala更改jdk版本 2024-1-2
  • 关于我的数据库——MySQL——第二篇
  • Qt/C++路径轨迹回放/回放每个点信号/回放结束信号/拿到移动的坐标点经纬度
  • JavaEE初阶---多线程(三)---内存可见性/单例模式/wait,notify的使用解决线程饿死问题
  • ubuntu虚拟机网络配置
  • C++STL之stack
  • 二十、行为型(访问者模式)
  • Java学习Day53:铲除紫云山金丹原料厂厂长(手机快速登录、权限控制)
  • 浅谈AI大模型的数据特点和应用问题
  • JavaEE初阶---多线程(五)---定时器/线程池介绍
  • 如何在国内安装使用Python,国内镜像站点加速库的安装
  • 用哪种建站程序做谷歌SEO更容易?
  • P6458 [COCI2006-2007#5] LIGA
  • 算法汇总整理篇——贪心与动态规划学习及框架思考
  • ReactNative 启动应用(2)
  • 【Linux操作系统】Linux配置OpenSSH服务器步骤记录
  • 【Linux】操作系统初步理解与Linux指令入门