1. 父组件中获取子组件方法

  • $children
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
    <div>
        <v-header></v-header>
        <v-content></v-content>
        <v-footer></v-footer>
    </div>
</template>
<script>
    import vHeader from './Header';
    import vContent from './Content';
    import vFooter from './Footer';
 
    export default {
        components:{vHeader,vContent,vFooter},
        created(){
            console.log(this.$children)
            //输出结果[VueComponent,VueComponent,VueComponent],此时可以通过下标获取响应组件,如获取vHeader为this.$children[0].
        }
    }
</script>
  • $refs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
    <div>
        <v-header ref='header'></v-header>
        <v-content ref='content'></v-content>
        <v-footer ref='footer'></v-footer>
    </div>
</template>
<script>
    import vHeader from './Header';
    import vContent from './Content';
    import vFooter from './Footer';
 
    export default {
        components:{vHeader,vContent,vFooter},
        created(){
            console.log(this.$refs);
            //输出结果:{header:VueComponent,content:VueComponent,footer:VueComponent},此时可以通过对象key进行获取响应组件,如vHeader组件获取为this.$refs.header
        }
    }
</script>

2. 子组件中定义父组件所要触发事件

  • methods直接定义
1
2
3
4
5
6
7
8
9
10
<script>
    export default {
        methods:{
            childAction(val='hello world'){
                console.log(val)
            }
            //此时在父组件,可以通过获取相应子组件,使用对象key值childAction对其进行调用,当前函数形参非必须
        }
    }
</script>
  • $on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script>
    export default {
        mounted(){
            this.$on('bridge',(val)=>{
                this.childAction(val);
            });
            ///此时通过$on进行监听中间桥接函数bridge对目的方法childAction进行触发
        },
        methods:{
            childAction(val='hello world'){
                console.log(val)
            }
 
        }
    }
</script>

3. 父组件调用子组件方法

  • 父组件Father.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<template>
    <div>
        <v-header ref='header'></v-header>
        <v-content ref='content'></v-content>
        <v-footer ref='footer'></v-footer>
        <button @click='emitChild1'>ref与on触发</button>
        <button @click='emitChild2'>ref直接触发</button>
        <button @click='emitChild3'>children与on触发</button>
        <button @click='emitChild4'>children直接触发</button>
    </div>
</template>
<script>
    import vHeader from './Header';
    import vContent from './Content';
    import vFooter from './Footer';
 
    export default {
        components:{vHeader,vContent,vFooter},
        methods:{
            emitChild1(){
                this.$refs.footer.$emit('bridge','你好吗!');
                //打印:  你好吗
                this.$refs.footer.$emit('bridge');
                //打印:hello world
            },
            emitChild2(){
                this.$refs.footer.childAction('你好吗!');
                //打印:  你好吗
                this.$refs.footer.childAction();
                //打印:hello world
            },
            emitChild3(){
                this.$children[2].$emit('bridge','你好吗!');
                //打印:  你好吗
                this.$children[2].$emit('bridge');
                //打印:hello world
            },
            emitChild4(){
                this.$children[2].childAction('你好吗!');
                //打印:  你好吗
                this.$children[2].childAction();
                //打印:hello world
            },
        }
    }
</script>
  • 子组件Footer.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<template>
    <footer>This is footer-component</footer>
</template>
<script>
    export default {
        mounted(){
            this.$on('bridge',(val)=>{
                this.childAction(val);
            });
 
        },
        methods:{
            childAction(val='hello world'){
                console.log(val)
            }
 
        }
    }
</script>

[转] vue父组件触发子组件事件的更多相关文章

  1. React 父组件触发子组件事件

    Parent组件 import React from "react"; import Child from "./component/Child"; class ...

  2. vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式

    vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式 class类组件示例 Father类组件 <template> & ...

  3. vue.js中父组件触发子组件中的方法

    知识点:vue.js中,父组件的method中,触发子组件中的方法,获得子组件中的定义的属性 (1)子组件 : child_crud.js var html_child_crud= "< ...

  4. 基于vue,通过父组件触发子组件的请求,等请求完毕以后,显示子组件,同时隐藏父组件

    正常情况下,子组件应该尽量减少业务逻辑,而应该将业务逻辑放到父组件里面,从而减少耦合,但是当 我们不得不用到这种情况时,可以采用下面的思路 解决方案 尽量将请求单独作为一个函数(不要将请求放到show ...

  5. vue父组件触发子组件方法

    比如应用场景是弹窗中的组件,想要点弹窗时更新该组件展示对应记录的的值 methods: { edit (record) { this.mdl = Object.assign({}, record) t ...

  6. vue 父组件与子组件的三生三世

    父组件和子组件相互传值:https://www.cnblogs.com/cxscode/p/11187989.html vue父组件触发子组件方法:https://www.cnblogs.com/cx ...

  7. Vee-validate 父组件获取子组件表单校验结果

    vee-validate 是为 Vue.js 量身打造的表单校验框架,允许您校验输入的内容并显示对应的错误提示信息.它内置了很多常见的校验规则,可以组合使用多种校验规则,大部分场景只需要配置就能实现开 ...

  8. 042——VUE中组件之子组件使用$on与$emit事件触发父组件实现购物车功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. vue 关于父组件无法触发子组件的事件的解决方法

    一般情况导致无法触发子组件的方法  基本都是由于子组件未渲染完成 就进行了调用,解决方法如下: 1.加定时器  setTimeout(() => { //加定时器原因是,子组件页面未渲染处理就做 ...

随机推荐

  1. Outlook API

    1.Outlook简介 若要从Outlook 外控制Outlook对象,必须在编写代码的工程中建立对Outlook对象库的引用. 1.1  Outlook Application说明: 代表整个Mic ...

  2. BUUCTF--checkin

    文件上传文件上传一般验证方式:1.本地js验证(客户端)2.MIME验证(服务端)3.拓展名验证(服务端)4.脚本内容(文件头)验证(服务端) 通常会用到exif_imagetype()函数,这个函数 ...

  3. maven 学习---Maven 构建配置文件

    什么是构建配置文件? 构建配置文件是一组配置的集合,用来设置或者覆盖 Maven 构建的默认配置.使用构建配置文件,可以为不同的环境定制构建过程,例如 Producation 和 Developmen ...

  4. 基于rtmp+nginx 、vlc实现FFmpeg推流与wpf端拉流

    这周在研究基于rtmp+nginx直播流的实现,现总结如下: 0.所需文件: 链接:https://pan.baidu.com/s/1U5gsNI8Rcl684l5gVL6swg 提取码:dli9 1 ...

  5. 深入理解JVM虚拟机(一):JVM运行时数据区

    概述: JVM将内存的管理进行封装,使得开发人员不必关心内存申请.释放操作.但是在高级程序开发.复杂业务场景开发的时候,如果出现内存溢出的情况,对于开发人员而言就很难去分析出原因.所以还是很有必要去了 ...

  6. ansible服务部署

    1.ansible.cfg配置文件 [defaults] #inventory= /home/op/ansible/testing #sudo_user=root remote_port=9122 r ...

  7. C# 上传本地视频到七牛云服务器

    第一步添加DLL引用 Install-Package Newtonsoft.Json Install-Package Qiniu #region 七牛云视频上传 /// <summary> ...

  8. Linux内存管理(最透彻的一篇)【转】

    转自:https://www.cnblogs.com/ralap7/p/9184773.html 摘要:本章首先以应用程序开发者的角度审视Linux的进程内存管理,在此基础上逐步深入到内核中讨论系统物 ...

  9. Python 简易web日志查看工具&可改装为命令行工具

    Python 简易web日志查看工具&可改装为命令行工具 效果图 原理 利用python的paramiko库模拟ssh登录操作,并执行tail命令 所需库 flask.paramiko.gev ...

  10. echarts之--柱状图-%显示

    测试地址 https://www.echartsjs.com/examples/zh/editor.html?c=bar-tick-align var option = { title: { text ...