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

VUE,子组件给父组件传递参数,props 自定义属性,ref

<template><div><!-- 子传父 --><!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 --><AA :getAAname="getAAname"/><h1>AA:{{aaname}}</h1><!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递参数(@或者v-on) --><BB v-on:eventBB="getBBname"/><h1>BB{{bbname}}</h1><!-- <BB v-on:eventBB.once="getBBname"/> --><!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递参数(使用ref实现 --><CC ref="CC"/><h1>CC{{ccname}}</h1></div>
</template><script>import AA from '@/components/lineComponent/AA.vue';import BB from '@/components/lineComponent/BB.vue';import CC from '@/components/lineComponent/CC.vue';export default {components: {AA,BB,CC},data() {return {aaname : '',bbname : '',ccname : '',}},methods: {// 01:AA使用props方法实现getAAname(val) {console.log('获取AA组件的名字',val)this.aaname=val},// 02:BB使用自定义事件实现getBBname(val){console.log('获取BB组件的名字1',val)this.bbname=val},// 注意1:参数比较多的时候// getBBname1(val1,val2){//     console.log('获取BB组件的名字1',val1,val2)// }// 注意2:参数比较多的时候  更推荐的写法// params是一个数组// getBBname1(val1,...params){//     console.log('获取BB组件的名字1',val1,params)// }      // 03:CC使用自定义事件实现getCCname(val){console.log('获取CC组件的名字1',val)this.ccname=val},},// 03:CC// 需求是等过了10秒才去获取BB中的参数// 更灵活的写法mounted () {this.$refs.CC.$on('eventCC',this.getCCname)// 注意// this.$refs.CC.$on('eventCC',function getCCname(){//     console.log(this)   //此处的this指的是 CC组件,而不是getAABB组件// })// setTimeout(()=>{//     console.log('可以了');// },10000)},// 只触发一次// this.$refs.CC.$once('eventCC',this.getCCname)}
</script>

AA组件

<template><div><div>我是A组件的数据{{name}}</div><button @click="sent">点击把AA组件的名字传递给父组件</button></div>
</template>
<script>export default {data() {return {name:"小艾",}},props: {getAAname: {},},methods: {sent() {this.getAAname(this.name)}},}
</script>

BB

<template><div><div>我是B组件的数据{{name}}</div><button @click="sent">点击把AA组件的名字传递给父组件</button><button @click="unbind">解绑</button></div>
</template>
<script>export default {data() {return {name: '小贝',age:19}},methods: {sent() {this.$emit("eventBB",this.name)},// 注意1:参数比较多的时候// sent1() {//     this.$emit("eventBB",this.name,this.age)// }// 注意2:参数很多// sent2() {//     this.$emit("eventBB",this.name,this.age)// }unbind(){// 解绑一个自定义事件this.$off('eventBB')// 解绑多个自定义事件// this.$off(['eventBB','eventBBB']);// 解绑所有的自定义事件// this.$off();  }},}
</script>

CC

<template><div><div>我是B组件的数据{{name}}</div><button @click="sent">点击把CC组件的名字传递给父组件</button></div>
</template>
<script>export default {data() {return {name: '小扣',age:19}},methods: {sent() {this.$emit("eventCC",this.name)},// 注意1:参数比较多的时候// sent1() {//     this.$emit("eventCC",this.name,this.age)// }// 注意2:参数很多// sent2() {//     this.$emit("eventCC",this.name,this.age)// }},}
</script>
http://www.lryc.cn/news/101919.html

相关文章:

  • 【Oracle系列】- Oracle数据迁移
  • Linux环境安装MySQL(详细教程)
  • 23. Mysql中的排序规则
  • MongoDB 基础学习记录
  • Visual Studio2022报错 无法打开 源 文件 “openssl/conf.h“解决方式
  • 【更新公告】Airtest更新至1.3.0.1版本
  • SQL语句集锦
  • 【多线程中的线程安全问题】线程互斥
  • 抖音seo短视频矩阵系统源代码开发技术分享
  • flutter实战(01)windows桌面版 修改应用logo、名称、显示位置、显示大小
  • 校园基础设施资源管理
  • Github git clone 和 git push 特别慢的解决办法
  • 【计网】TCP在可靠传输中都干了啥
  • windows下载安装FFmpeg
  • SwipeDelMenuLayout失效:Could not find SwipeDelMenuLayout-V1.3.0.jar
  • C++ 类和对象篇(零) 面向过程 和 面向对象
  • 列表list
  • gcc编译出现bar causes a section type conflict with foo问题解决
  • 12. Mybatis 多表查询 动态 SQL
  • 操作系统专栏1-内存管理from 小林coding
  • SpringCloud远程服务调用
  • Arcgis通过模型构建器计算几何坐标
  • java设计模式-工厂模式(下)
  • 深蓝学院C++基础与深度解析笔记 第13章 模板
  • 装饰器模式——扩展系统功能
  • 无涯教程-jQuery - jQuery.get( url, data, callback, type )方法函数
  • 【Vue3】递归组件
  • 【Python】数据分析+数据挖掘——探索Pandas中的索引与数据组织
  • matlab进阶:求解在约束条件下的多元目标函数最值(fmincon函数详解)
  • Kotlin知识点