vue父组件中获取子组件中的数据(实例讲解)
作者:jingxian 时间:2024-04-10 13:49:24
如下所示:
<FormItem label="上传头像" prop="image">
<uploadImg :width="150" :height="150" :name="'avatar'" size="150px*150px" ref="avatar"></uploadImg>
</FormItem>
<FormItem label="上传营业执照" prop="businessLicence">
<uploadImg :width="350" :height="200" :name="'businessLicence'" size="350px*200px" ref="businessLicence"></uploadImg>
</FormItem>
自己写了个上传图片的子组件,父组件需要获取到子组件上传的图片地址,
方法一:给相应的子组件加ref
父组件在最后提交的时候获取thi.$ref.avatar.相应数据即可,因为在这里才能保证图片已经上传,否则如果图片没上传,拿到的值一定为空。
方法二:$emit()
/*
子组件
*/
<template>
<input type='file' @change="changeUrl" />
</template>
<script>
export default {
methods: {
changeUrl(e) {
this.$emit('changeUrl', e.currentTarget.files[0].path)
}
}
}
</script>
/*
父组件
*/
<template>
<FormItem label="上传营业执照" prop="businessLicence">
<uploadImg :width="350" :height="200" :name="'license'" size="350px*200px" @changeUrl="getUrl"></uploadImg>
</FormItem>
</template>
<script>
export default {
methods: {
getUrl(path) {
//这个就是你要的path,并且会双向绑定
}
}
}
</script>
来源:http://www.cnblogs.com/beileixinqing/archive/2017/09/26/7597647.html
标签:vue,父组件,获取,子组件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql 事务处理及表锁定深入简析
2024-01-23 06:34:52
Python解决两个整数相除只得到整数部分的实例
2021-12-29 23:29:21
Django 解决开发自定义抛出异常的问题
2023-03-05 12:43:55
Python中asyncio模块的深入讲解
2022-05-18 22:33:53
Python字体反爬实战案例分享
2021-06-18 01:00:46
![](https://img.aspxhome.com/file/2023/9/75929_0s.png)
Python中的np.vstack()和np.hstack()详解
2022-05-09 03:06:56
MySQL图形化管理工具Navicat安装步骤
2024-01-29 05:00:45
![](https://img.aspxhome.com/file/2023/9/121239_0s.png)
一篇文章教会你PYcharm的用法
2023-06-30 14:18:56
![](https://img.aspxhome.com/file/2023/3/114653_0s.jpg)
浅谈JavaScript的自动垃圾收集机制
2023-08-13 15:18:57
python中map、any、all函数用法分析
2021-09-02 15:25:45
java自定义动态链接数据库示例
2024-01-27 13:05:29
MySQL执行时间的查询
2024-01-14 13:54:25
![](https://img.aspxhome.com/file/2023/1/69751_0s.jpg)
ASP 多关键词查询实例代码
2011-04-11 11:14:00
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
2021-05-19 06:09:39
如何用python实现一个HTTP连接池
2021-10-20 04:52:03
![](https://img.aspxhome.com/file/2023/5/108155_0s.png)
Python中面向对象你应该知道的一下知识
2022-01-31 11:35:47
scrapy数据存储在mysql数据库的两种方式(同步和异步)
2023-07-10 03:29:43
![](https://img.aspxhome.com/file/2023/4/62304_0s.png)
Python+腾讯云服务器实现每日自动健康打卡
2023-08-18 00:22:44
![](https://img.aspxhome.com/file/2023/8/70678_0s.png)
Python中使用多进程来实现并行处理的方法小结
2023-10-23 14:21:38
MSSQL段落还原脚本,SQLSERVER段落脚本
2024-01-22 14:48:15