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

【Vue】父子组件值及方法传递使用

父子组件值、方法引用

1、值

1.1 父组件获取子组件值

父组件

<template><div><button @click="getChildValue">click</button><child ref="child"></child></div>
</template><script>
import Child from './Child.vue'export default {name: "Parent",components: {Child},data() {return {}},methods: {getChildValue() {console.log(this.$refs.child.msg1)}}
}
</script>

子组件

<template><div id="child"><div>{{ msg1 }}</div></div>
</template><script>
export default {name: "Child",data() {return {msg1: "i am child"};},methods: {}
}
</script>

测试结果在控制台查看

1.2 子组件获取父组件值

方式1:

props: ['parentName']

方式2:

props: {parentName: String //这样可以指定传入的类型,如果类型不对,会警告
}

方式3:

props: {msgVal: {type: String, //指定传入的类型//type 也可以是一个自定义构造器函数,使用 instanceof 检测。default: '' //这样可以指定默认的值}
}

注意:props 会在组件实例创建之前进行校验,所以在 default 或 validator 函数里,
诸如 data、computed 或 methods 等实例属性还无法使用

父组件

<template><div><child ref="child" :parentName="parentName"></child></div>
</template><script>
import Child from './Child.vue'export default {name: "Parent",components: {Child},data() {return {parentName:'i am parent'}},methods: {}
}
</script>

子组件

<template><div id="child"><button @click="getParentsValue">获取父组件值</button></div>
</template><script>
export default {name: "Child",props:['parentName'],data() {return {};},methods: {getParentsValue(){console.log(this.parentName)}}
}
</script>

2、方法

2.1 父组件调用子组件方法

父组件

<template><div><button @click="clickChildMethodNoParams">调用子组件方法 无参数 </button><button @click="clickChildMethodParams">调用子组件方法 有参数 </button><child ref="child" ></child></div>
</template><script>
import Child from './Child.vue'
export default {name: "Parent",components: {Child},data() {return {}},methods: {clickChildMethodNoParams(){this.$refs.child.childMethod();},clickChildMethodParams(){this.$refs.child.childMethodAddParams(' i am parent ');}}
}
</script>

子组件

<template><div id="child"><p> this is child compoents </p></div>
</template><script>
export default {name: "Child",data() {return {};},methods: {childMethod(){console.log('i am child method')},childMethodAddParams(param){console.log('i am child method ',param)}}
}
</script>

2.2 子组件调用父组件方法

父组件

<template><div><child ref="child"@clickChildMethodNoParams="clickChildMethodNoParams"@clickChildMethodParams="clickChildMethodParams"></child></div>
</template><script>
import Child from './Child.vue'export default {name: "Parent",components: {Child},data() {return {}},methods: {clickChildMethodNoParams(){console.log('i am parent')},clickChildMethodParams(params){console.log('i am parent '+ params)}}
}
</script>

子组件

<template><div id="child"><button @click="childParentMethodNoParams">调用父组件方法 无参数 </button><button @click="childParentMethodParams">调用父组件方法 有参数 </button><p> this is child compoents </p></div>
</template><script>
export default {name: "Child",data() {return {};},methods: {childParentMethodNoParams(){this.$emit('clickChildMethodNoParams');},childParentMethodParams(){this.$emit('clickChildMethodParams','hello i am child ');}}
}
</script>
http://www.lryc.cn/news/107125.html

相关文章:

  • 医药化工企业洁净厂房改造消防防爆安全的重要性
  • Web开发中防止SQL注入
  • 【LeetCode-中等】剑指 Offer 35. 复杂链表的复制(详解)
  • QT图形视图系统 - 使用一个项目来学习QT的图形视图框架 -第一篇
  • Cat.1如何成为物联网业务加速器?
  • Qt应用开发(基础篇)——布局管理 Layout Management
  • Python web实战之 Django 的 ORM 框架详解
  • pycharm制作柱状图
  • 静态资源导入探究
  • 安全狗V3.512048版本绕过
  • prometheus监控k8s kube-proxy target down
  • SPSS数据分析--假设检验的两种原假设取舍决定方式
  • Python实现猫狗分类
  • pjsip、pjsua2+bcg729 windows下编译java版本
  • 尝试多数据表 sqlite
  • Keil出现Flash Timeout.Reset the Target and try it again.我有一种解决方法
  • 纯粹即刻,畅享音乐搜索的轻松体验
  • 动态规划之树形DP
  • 嵌入式_GD32使用宏开关进行Debug串口打印调试
  • 使用 GitHub Copilot 进行 Prompt Engineering 的初学者指南(译)
  • c++开发模式,享元模式
  • LLM大模型——langchain相关知识总结
  • 【Python】数据可视化利器PyCharts在测试工作中的应用
  • AOP的实战(统一功能处理模块)
  • 时间复杂度为O(n2)的三种简单排序算法
  • LeetCode 热题 100 JavaScript --226. 翻转二叉树
  • hive所有窗口函数详情总结
  • Talk | 新加坡国立大学博士生施宇钧:DragDiffusion-基于扩散模型的关键点拖拽图片编辑
  • 22 | 贝叶斯分类算法
  • java.sql.SQLSyntaxErrorException: ORA-00909: 参数个数无效