美文网首页Vue.js专区
vue父组件调用子组件内部方法

vue父组件调用子组件内部方法

作者: SuperBinlin | 来源:发表于2017-05-23 15:24 被阅读633次

    今天同事用iview开发时,需要用到iview一个组件的内部方法,而这个内部方法并没有暴露出来,这种情况下如何调用组件内部方法呢,其实很简单,举个栗子🌰

    子组件:

    <template>
        <div>
            child
        </div>
    </template>
    
    <script>
        export default {
            name: "child",
            props: "someprops",
            methods: {
                parentHandleclick(e) {
                    console.log(e)
                }
            }
        }
    </script>
    

    父组件:

    <template>
        <div>
            <button @click="clickParent">点击</button>
            <child ref="mychild"></child>
        </div>
    </template>
    
    <script>
        import Child from './child';
        export default {
            name: "parent",
            components: {
                child: Child
            },
            methods: {
                clickParent() {
                    this.$refs.mychild.parentHandleclick("嘿嘿嘿");
                }
            }
        }
    </script>
    

    当然,如果是自己开发组件时,父组件和子组件有很多方法可以通信~

    相关文章

      网友评论

        本文标题:vue父组件调用子组件内部方法

        本文链接:https://www.haomeiwen.com/subject/cgxgxxtx.html