您好,欢迎访问一九零五行业门户网

vue 父子组件相互调用的实现

这次给大家带来vue 父子组件相互调用的实现,vue 父子组件相互调用的注意事项有哪些,下面就是实战案例,一起来看一下。
情景:
父组件中引入上传附件的子组件:点击组件可以分别上传对应要求的图片,子组件内部循环可创建多个模块.
父组件传入数组子组件循环来创建不同的组件模块,所有事件都在子组件内部.
父组件页面的上方同时有一个上传图片按钮上传图片后会显示在第一个模块:
设想思路:点击父组件中的按钮触发子组件中上传方法:
子组件上定义ref=refname,父组件的方法中用this.$refs.refname.method去调用子组件方法
子组件中处理上传的方法:
fileclick(index) {    console.log('子组件的fileclick被调用了')    console.log('index:  '+index)    // this.aaa();    if(!this.fileinfor[index].imgurl){    //如果当前框里没有图片,则实现上传    document.getelementsbyclassname('upload_file')[index].click();   }     },
父组件template
<template>   <x-button type="submit" class="custom-primary" @click.native="xiechengupload">上传图片</x-button>   <up-load :fileinformation="fileinformation" ref="uploadref"></up-load> </template>
父组件method中定义方法,同时传入相应的index值.
upload(){   // console.log('父组件的xiechengupload被调用了')   this.$refs.uploadref.fileclick(0); },
此时就可以通过上传按钮将图片放到子组件的第一个模块中了.
下面看下vue父组件调用子组件事件
vue父组件向子组件传递事件/调用事件
不是传递数据(props)哦,适用于 vue 2.0
方法一:子组件监听父组件发送的方法
方法二:父组件调用子组件方法
子组件:
export default {   mounted: function () {    this.$nexttick(function () {     this.$on('childmethod', function () {      console.log('监听成功')     })    })   },   methods {     callmethod () {      console.log('调用成功')     }   } }
父组件:
<child ref="child" @click="click"></child> export default {   methods: {    click () {    this.$refs.child.$emit('childmethod') // 方法1    this.$refs.child.callmethod() // 方法2   },   components: {    child: child   } }
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue实现图片与文件上传步骤详解
axios发送post请求提交图片表单步骤详解
以上就是vue 父子组件相互调用的实现的详细内容。
其它类似信息

推荐信息