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

vue中,父子组件传递参数 props 实现方式

通过 Prop 向子组件传递数据

001:父组件=====》子组件通信

<template><div><h1>这里是父元素</h1>//******<childComponent :detailMes="detailMes"/></div>
</template><script>import childComponent from  './childComponent'export default {name:"parentComponent",data() {return {detailMes:'111'}},components: {childComponent,},}
</script>

子组件

<template><div>数据需要从父元素传递过来哦:{{detailMes}}</div>
</template><script>export default {name:'childComponent',data() {return {}},props: {detailMes:{type : String,}},methods: {name() {}}}

002:子组件=====》父组件通信
(要求父组件先给子组件一个函数)
列表数据在父组件,循环的ul>li在子组件,现在在组件删除数据,需要通知父组件

<template><div><h1>这里是父</h1>//父组件先给子组件一个函数<childComponent :list="list" :del="del"/></div>
</template><script>import childComponent from  './childComponent'export default {data() {return {list:[{id:"001",stuName:'学生a'},{id:"002",stuName:'学生b'},]}},components: {childComponent,},methods: {del(id) {const idx=this.list.findIndex(v=>v.id==id);if(idx>-1){this.list.splice(idx,1)}// this.list=this.list.filter(item=>item.id!==id)}},}
</script>
<template><div>子组件<ul><li v-for="item of list" :key="item.id"><span>{{item.stuName}}</span><button @click="dele(item.id)" class="red">x</button></li></ul></div>
</template><script>export default {name:'childComponent',data() {return {}},props: {// 父元素传递过来的方法list:{},// 父组件传递过来的方法del:{}},methods: {dele(ids) {// 通知父元素,快删除数据了// 去调用父组件的方法this.del(ids);}}}

003 传递 多层传递下去

<template><div><h1>这里是父</h1><childComponent :msg="msg"/></div>
</template><script>import childComponent from  './childComponent'export default {data() {return {msg:'这条数据需要通过层层传递下去'}},components: {childComponent,},}
</script>
<template><div>子组件<grandsonComponent :msg="msg"></grandsonComponent></div>
</template><script>
import grandsonComponent from '@/components/grandsonComponent.vue';export default {components: {grandsonComponent,},props: {msg:{}},}
</script>
<template><div>这是统计的{{msg}}的数据</div>
</template><script>export default {name:'grandsonComponent',props: {msg: {}},}
</script>
http://www.lryc.cn/news/101098.html

相关文章:

  • Unity如何快速接入iOS和GooglePlay的成就排行榜等GameCenter功能
  • Unity下如何实现低延迟的全景RTMP|RTSP流渲染
  • STM32 USB使用记录:HID类设备(后篇)
  • C# 快速写入日志 不卡线程 生产者 消费者模式
  • Pandas将对角线元素设为1
  • WPF实战学习笔记28-登录界面
  • 01背包
  • 064、故障处理之OMM_TiDB
  • 网络设备中的配置文件管理
  • HCIP BGP综合实验
  • 【mysql学习篇】Order by与Group by优化以及排序算法详解
  • 【业务功能篇60】Springboot + Spring Security 权限管理 【终篇】
  • 文章详情页 - 评论功能的实现
  • 使用贝叶斯滤波器通过运动模型和嘈杂的墙壁传感器定位机器人研究(Matlab代码实现)
  • Day 69-70:矩阵分解
  • 数据结构:树的存储结构
  • Vue前端渲染blob二进制对象图片的方法
  • Java的标记接口(Marker Interface)
  • Kafka基础架构与核心概念
  • 观察者模式与观察者模式实例EventBus
  • 科普 | OSI模型
  • redis相关异常之RedisConnectionExceptionRedisCommandTimeoutException
  • Merge the squares! 2023牛客暑期多校训练营4-H
  • STM32 串口学习(二)
  • 点大商城V2_2.5.0 全开源版 商家自营+多商户入驻 百度+支付宝+QQ+头条+小程序端+unipp开源前端安装测试教程
  • “深入理解SpringBoot:从入门到精通“
  • PCB绘制时踩的坑 - SOT-223封装
  • Go语法入门 + 项目实战
  • QT控件通过qss设置子控件的对齐方式、大小自适应等
  • 基于java在线收银系统设计与实现