Vue $emit $refs子父组件间方法的调用
来源:互联网 发布:lol免费刷皮肤软件 编辑:程序博客网 时间:2024/06/08 14:01
1、$emit
子组件调用父组件的方法并传递数据
注意:子组件标签中的时间也不区分大小写要用“-”隔开
子组件:
<template> <button @click="emitEvent">点击我</button></template><script> export default { data() { return { msg: "我是子组件中的数据" } }, methods: { emitEvent(){ this.$emit('my-event', this.msg) //通过按钮的点击事件触发方法,然后用$emit触发一个my-event的自定义方法,传递this.msg数据。 } } }</script>
父组件:
<template> <div id="app"> <child-a @my-event="getMyEvent"></child-a> <!--父组件中通过监测my-event事件执行一个方法,然后取到子组件中传递过来的值--> </div></template><script> import ChildA from './components/child.vue' export default { components: { ChildA }, methods: { getMyEvent(msg){ console.log('接收的数据--------->'+msg)//接收的数据--------->我是子组件中的数据 } } }</script>
2、$refs
父组件调用子组件的方法,可以传递数据
注意:子组件标签中的时间也不区分大小写要用“-”隔开
父组件:
<template> <div id="app"> <child-a ref="child"></child-a> <!--用ref给子组件起个名字--> <button @click="getMyEvent">点击父组件</button> </div></template><script> import ChildA from './components/child.vue' export default { components: { ChildA }, data() { return { msg: "我是父组件中的数据" } }, methods: { getMyEvent(){ this.$refs.child.emitEvent(this.msg); //调用子组件的方法,child是上边ref起的名字,emitEvent是子组件的方法。 } } }</script>
子组件:
<template> <button>点击我</button></template><script> export default { methods: { emitEvent(msg){ console.log('接收的数据--------->'+msg)//接收的数据--------->我是父组件中的数据 } } }</script>
阅读全文
0 0
- Vue $emit $refs子父组件间方法的调用
- vue---父调子 $refs (把父组件的数据传给子组件)子调父 $emit (把子组件的数据传给父组件)
- vue 父组件调用子组件方法
- vue关于父组件调用子组件的方法
- vue使用$emit时,父组件无法监听到子组件的事件
- vue 2.0 父组件调用子组件中的方法
- vue 父组件调用子组件方法解决方案
- Vue父组件调用子组件事件
- 子组件调用父组件的方法
- 子组件调用父组件的方法
- vue组件之间的通信以及如何在父组件中调用子组件的方法和属性
- vue组件($children,$refs,$parent)的使用
- vue组件($children,$refs,$parent)的使用
- vue组件中$emit()的作用
- vue组件之emit
- vue子组件获取父组件方法
- 父组件调用子组件的方法以及子组件调用父组件的方法
- vue2.0子组件向父组件传递数据(饿了么例子)vue2.0废弃$dispatch,替换成$emit 和$refs
- Python报错'module' object is not callable
- 17. Letter Combinations of a Phone Number
- 机器学习作业2
- Intellij IDEA 安装lombok及使用详解
- java中的String.join
- Vue $emit $refs子父组件间方法的调用
- HDU 6116 路径计数(组合数学+NTT)
- Android学习笔记16---星级评分条的实现
- 开启博客之旅
- 1098: 树状数组2(破坏公路)
- 第十三课 创建高级联结
- Linux环境下配置出现下列问题
- 读《大话处理器 》
- OpenCV学习之旅——滑动条的创建和使用