美文网首页
VUE中父组件使用子组件函数,子组件使用父组件函数

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

作者: 冰凉ming | 来源:发表于2020-01-16 19:02 被阅读0次

(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.方法

相关文章

  • Vue3 emit使用

    父组件: ①定义函数②使用子组件时使用该函数 子组件: ①子组件引入defineEmits②赋值给emit③触发

  • uniapp 父组件与 renderjs 子组件通信

    父组件向子组件通信 使用 prop 传递数据,子组件监听数据变化 子组件向子组件通信 父组件创建回调函数,子组件特...

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

    (1)vue中父组件调用子组件函数 用法:子组件上定义ref="refName", 父组件的方法中用this.$r...

  • vue中父子组件传值

    vue中父子组件传值是经常使用的场景 父组件向子组件中传值 父组件 子组件 父组件向子组件中传值分三步1、在父组件...

  • vue---子父、父子、非父子组件通信

    子组件和父组件通信 在父组件中使用子组件时自定义事件,设置该事件的回调函数 在子组件中需要传数据给父组件时调用th...

  • vue子父组件通信

    子父组件传递数据 父组件===》子组件。父组件通过props向子组件中传递数据和改变数据的函数,通过在子组件中调用...

  • react组件传值

    一、父组件向子组件传值1.父组件中 2.子组件中 二、子组件向父组件传值 1.父组件中,创建一个函数传入子组件中 ...

  • Vue高级特性「六」--slot 插槽

    基本使用 父组件在引用的子组件中添加内容 子组件 slotDemo.vue 作用域插槽 父组件可以拿到子组件的数据...

  • react 父子组件通信

    1. 子组件拿到父组件数据 在父组件中定义一个函数,将其传递到子组件中,子组件调用这个回调函数就可以拿到父组件中的...

  • vue知识点之组件通信

    父组件 --> 子组件(1) props:父组件可以使用props向子组件传递数据父组件: 子组件: (2) 使用...

网友评论

      本文标题:VUE中父组件使用子组件函数,子组件使用父组件函数

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