zoukankan      html  css  js  c++  java
  • VUE父组件调用子组件方法

    父组件

    <template>
        <div>
            <div @click="click">点击父组件</div>
            <child ref="child"></child>
        </div>
    </template>
    
    <script>
        import child from "./child";
        export default {
            methods: {
                click() {
                    this.$refs.child.$emit('childMethod','发送给方法一的数据') // 方法1:触发监听事件
                    this.$refs.child.callMethod() // 方法2:直接调用
                },
            },
            components: {
                child,
            }
        }
    </script>

    子组件

    <template>
        <div>子组件</div>
    </template>
    
    <script>
        export default {
            mounted() {
                this.monitoring() // 注册监听事件
            },
            methods: {
                monitoring() { // 监听事件
                    this.$on('childMethod', (res) => {
                        console.log('方法1:触发监听事件监听成功')
                        console.log(res)
                    })
                },
                callMethod() {
                    console.log('方法2:直接调用调用成功')
                },
            }
        }
    </script>
    
    
  • 相关阅读:
    2015多校1006.First One
    2015多校.MZL's endless loop(欧拉回路的机智应用 || 构造)
    LUXURY 8
    矩阵快速幂模板
    博弈入门
    cf558c(bfs)
    LUXURY 7
    dfs序 + RMQ = LCA
    双端队列
    UVa-401 Palindromes
  • 原文地址:https://www.cnblogs.com/hurenjie/p/13844825.html
Copyright © 2011-2022 走看看