vue.js $refs和$emit 父子组件交互的方法
作者:axl234 时间:2024-04-30 10:19:11
本文介绍了vue.js $refs和$emit 父子组件交互的方法,分享给大家,废话不多说直接看代码:
<strong>父调子 $refs (把父组件的数据传给子组件) </strong><br><br><template>
<div id="app">
<input type="button" name="" id="" @click="parentCall" value="父调子" />
<hello ref="chil" />//hello组件
</div>
</template>
<script>
import hello from './components/Hello'
export default {
name: 'app',
'components': {
hello
},
methods: {
parentCall () {
this.$refs.chil.chilFn('我是父元素传过来的')
}
}
}
</script>
/*Hello.vue :*/
<template>
<div class="hello"></div>
</template>
<script>
export default {
name: 'hello',
'methods': {
chilFn (msg) {
alert(msg)
}
}
}
</script>
<strong>子调父 $emit (把子组件的数据传给父组件)</strong>
//ps:App.vue 父组件
//Hello.vue 子组件
<!--App.vue :-->
<template>
<div id="app">
<hello @newNodeEvent="parentLisen" />
</div>
</template>
<script>
import hello from './components/Hello'
export default {
name: 'app',
'components': {
hello
},
methods: {
parentLisen(evtValue) {
//evtValue 是子组件传过来的值
alert(evtValue)
}
}
}
</script>
<!--Hello.vue :-->
<template>
<div class="hello">
<input type="button" name="" id="" @click="chilCall()" value="子调父" />
</div>
</template>
<script>
export default {
name: 'hello',
'methods': {
chilCall(pars) {
this.$emit('newNodeEvent', '我是子元素传过来的')
}
}
}
</script>
来源:https://www.cnblogs.com/axl234/p/7797247.html
标签:vue,refs,emit
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
css行高:line-height属性详解
2008-06-24 11:42:00
![](https://img.aspxhome.com/file/UploadPic/20086/24/2008624115434297s.gif)
python 实现 redis 数据库的操作
2024-01-16 07:46:08
详解go-admin在线开发平台学习(安装、配置、启动)
2023-08-26 15:27:31
![](https://img.aspxhome.com/file/2023/9/101049_0s.png)
PHP实现加减乘除最简单的实例分享
2023-06-14 16:17:41
Python爬虫实现搭建代理ip池
2022-05-19 06:57:11
![](https://img.aspxhome.com/file/2023/8/105618_0s.png)
MySQL DISTINCT 的基本实现原理详解
2024-01-15 17:21:29
Scrapy元素选择器Xpath用法汇总
2021-09-11 11:42:58
MSSQL差异备份取系统权限的相关软件下载
2024-01-21 16:08:46
Python3.5局部变量与全局变量作用域实例分析
2023-05-20 23:18:28
保护Access 2000数据库的安全
2008-10-23 13:55:00
取巧的边框等高
2009-12-16 12:11:00
![](https://img.aspxhome.com/file/UploadPic/200912/16/css_border-13s.png)
在python的WEB框架Flask中使用多个配置文件的解决方法
2023-09-05 03:56:54
java连接Oracle数据库的方法解析
2024-01-21 22:12:27
![](https://img.aspxhome.com/file/2023/4/65584_0s.png)
python实现字符串连接的三种方法及其效率、适用场景详解
2023-07-27 19:57:32
关于windows下Tensorflow和pytorch安装教程
2023-06-09 07:35:54
![](https://img.aspxhome.com/file/2023/5/70695_0s.png)
python3实现指定目录下文件sha256及文件大小统计
2023-11-26 15:37:00
![](https://img.aspxhome.com/file/2023/2/97612_0s.png)
Python统计列表中的重复项出现的次数的方法
2023-10-12 08:06:15
Python 求向量的余弦值操作
2022-11-24 22:51:11
![](https://img.aspxhome.com/file/2023/7/90697_0s.jpg)
Vue生产和开发环境如何切换及过滤器的使用
2024-05-09 15:20:31
40个你可能不知道的Python技巧附代码
2021-09-26 13:56:58