VUE中父组件使用子组件函数,子组件使用父组件函数

2020-01-16  本文已影响0人  冰凉ming

(1)vue中父组件调用子组件函数

用法: 子组件上定义ref="refName", 父组件的方法中用 this.$refs.refName.method 去调用子组件方法

详解: 父组件里面调用子组件的函数,父组件先把函数/方法以属性形式传给子组件;那么就需要先找到子组件对象 ,即 this.$refs.refName 。 然后再进行调用,也就是 this.$refs.refName.method

子组件:

<template>

    <div> childComponent </div> 

</template> 

<script>

export default { 

    name: "child",

    methods: { 

        childClick(e) { console.log(e) }

     }

}

</script>

父组件:

<template>

    <div>

        <button @click="parentClick">

            点击

        </button>

        <Child ref="mychild"/>    // 使用组件标签

    </div>

</template>

<script>

import Child from './child';    //引入子组件Child

export default {

    name:  "parent",

    components: {

        Child    // 将组件隐射为标签

    },

    methods: {

        parentClick () {

            this.$refs.mychild.childClick( "我是子组件里面的方法哦" );  // 调用子组件的方法 childClick

        }

    }

}

</script>

(2)vue中子组件调用父组件函数

方法一:

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

父组件:

<template>

     <div>

         <child></child>

    </div>

</template>

<script>

     import child from '~/components/dam/child';

     export default {

         components: {

             child

         },

         methods: { 

             fatherMethod() { 

                 console.log('测试'); 

             } 

         }

     };

</script>

子组件:

<template> 

     <div>

         <button @click="childMethod()">

                点击

        </button>

     </div>

</template>

<script>

export default { 

     methods: { 

         childMethod() {

             this.$parent.fatherMethod();

         } 

     }

};

</script>

方法二:

在子组件里用$emit向父组件触发一个事件,父组件监听这个事件就行了

父组件:

<template>

     <div>

         <child @fatherMethod="fatherMethod"></child>

     </div>

</template>

<script>

     import child from '~/components/dam/child'; 

export default { 

     components: { 

         child 

     },

     methods: {

         fatherMethod() { 

             console.log('测试'); 

         } 

     } 

}; 

</script>

子组件:

<template>

     <div>

         <button @click="childMethod()">

            点击

        </button>

     </div> 

</template> 

<script>

export default { 

     methods: { 

         childMethod() { 

             this.$emit('fatherMethod'); 

         }

     } 

};

</script>

方法三

把方法传入子组件中,在子组件里直接调用这个方法。

父组件:

<template>

     <div> 

         <child :fatherMethod="fatherMethod"></child>  

    </div> 

</template>

<script> 

import child from '~/components/dam/child'; 

export default { 

     components: {

         child 

     },

     methods: { 

         fatherMethod() { 

             console.log('测试');

         } 

     } 

}; 

</script>

子组件:

<template> 

     <div>

         <button @click="childMethod()">

            点击

        </button> 

     </div>

</template> 

<script> 

export default { 

     props: { 

         fatherMethod: { 

             type: Function, 

             default: null

         }

     }, 

     methods: { 

         childMethod() { 

             if (this.fatherMethod) {

                 this.fatherMethod(); 

             } 

         }

     }

};

</script>

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

父组件主动获取子组件的数据和方法

1.调用子组件的时候 定义一个ref

<headerchild ref="headerChild"></headerchild>

2.在父组件里面通过

this.$refs.headerChild.属性

this.$refs.headerChild.方法

子组件主动获取子组件的数据和方法

在子组件里面通过以下方式调用

this.$parent.属性

this.$parent.方法

上一篇下一篇

猜你喜欢

热点阅读