前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue子组件调用父组件中方法的方式合集

vue子组件调用父组件中方法的方式合集

作者头像
用户10106350
发布2022-10-28 11:26:15
8120
发布2022-10-28 11:26:15
举报
文章被收录于专栏:WflynnWebWflynnWeb

第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法

父组件代码

代码语言:javascript
复制
<template>
    <div>
        <songCompoent></songCompoent>
    </div>
</template>

<script>
    const songCompoent = () => import('@testCp/songCompoent')

    export default {
        methods: {
            fatherFnOne () {
                console.log('我是父方法1')
            },
        },
        components: {
            songCompoent
        },
    }
</script>

子组件代码

代码语言:javascript
复制
<template>
    <div>
        <button @click="sonFn">我是调用父组件方法的按钮</button>
    </div>
</template>

<script>
    export default {
        methods: {
            sonFn () {
                this.$parent.fatherFnOne() // 调用父组件的fatherFnOne方法
            },
        },
    }
</script>

第二种方法是在子组件里用$emit向父组件触发一个事件,父组件监听这个事件就行了。

父组件代码

代码语言:javascript
复制
<template>
    <div>
        <songCompoent @fatherFn="fatherFnTwo"></songCompoent>
    </div>
</template>

<script>
    const songCompoent = () => import('@testCp/songCompoent')

    export default {
        methods: {
            fatherFnTwo () {
                console.log('我是父方法2')
            },
        },
        components: {
            songCompoent
        },
    }
</script>

子组件代码

代码语言:javascript
复制
<template>
    <div>
        <button @click="sonFn">我是调用父组件方法的按钮</button>
    </div>
</template>

<script>
    export default {
        methods: {
            sonFn () {
                this.$emit('fatherFn') // 调用父组件的fatherFnTwo方法
            },
        },
    }
</script>

第三种是父组件把方法传入子组件中,在子组件里直接调用这个方法

父组件代码

代码语言:javascript
复制
<template>
    <div>
        <songCompoent :fatherFnThree="fatherFnThree"></songCompoent>
    </div>
</template>

<script>
    const songCompoent = () => import('@testCp/songCompoent')

    export default {
        methods: {
            fatherFnThree () {
                console.log('我是父方法3')
            },
        },
        components: {
            songCompoent
        },
    }
</script>

子组件代码

代码语言:javascript
复制
<template>
    <div>
        <button @click="sonFn">我是调用父组件方法的按钮</button>
    </div>
</template>

<script>
    export default {
        props: { // String Number Boolean Array Object Function || validator (value) {}
            fatherFnThree: { // 父组件传过来的方法 fatherFnThree
                type: Function,
                required: false,
                default: function () {

                }
            },

        },
        methods: {
            sonFn () {
                this.fatherFnThree() // 调用父组件的fatherFnThree方法
            },
        },
    }
</script>

综合代码

父组件

代码语言:javascript
复制
<template>
    <div>
        <songCompoent @fatherFn="fatherFnTwo" :fatherFnThree="fatherFnThree"></songCompoent>
    </div>
</template>

<script>
    const songCompoent = () => import('@testCp/songCompoent')

    export default {
        methods: {
            fatherFnOne () {
                console.log('我是父方法1')
            },
            fatherFnTwo () {
                console.log('我是父方法2')
            },
            fatherFnThree () {
                console.log('我是父方法3')
            },
        },
        components: {
            songCompoent
        },
    }
</script>

子组件

代码语言:javascript
复制
<template>
    <div>
        <button @click="sonFn">我是调用父组件方法的按钮</button>
    </div>
</template>

<script>
    export default {
        props: { // String Number Boolean Array Object Function || validator (value) {}
            fatherFnThree: { // 父组件传过来的方法 fatherFnThree
                type: Function,
                required: false,
                default: function () {

                }
            },

        },
        methods: {
            sonFn () {
                this.$parent.fatherFnOne() // 调用父组件的fatherFnOne方法
                this.$emit('fatherFn') // 调用父组件的fatherFnTwo方法
                this.fatherFnThree() // 调用父组件的fatherFnThree方法
            },
        },
    }
</script>

消息中间件之ActiveMQ

链接:https://pan.baidu.com/s/16vCBGIl5hN_1JTQy8NZ2Vg

提取码:1443

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WflynnWeb 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第二种方法是在子组件里用$emit向父组件触发一个事件,父组件监听这个事件就行了。
  • 第三种是父组件把方法传入子组件中,在子组件里直接调用这个方法
  • 综合代码
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档