美文网首页前端学习记录
Vue子父组件方法互调

Vue子父组件方法互调

作者: 无言以越 | 来源:发表于2019-07-22 18:43 被阅读0次

讲干货,不啰嗦,大家在做vue开发过程中经常遇到父组件需要调用子组件方法或者子组件需要调用父组件的方法的情况,现做一下总结,希望对大家有所帮助。

父组件调用子组件方法:

1.设置子组件的ref,父组件通过this.$refs.xxx.method_name(data)调用子组件方法,data参数可选

父组件:

<template>

  <div>

  <h1>我是父组件</h1>

    <child ref="childname"></child>

  </div>

</template>

<script>

  import child from '~/components/child';

  export default {

    components: {

      child

    },

    methods: {

      fatherMethod(data)

        this.$refs.childname.childMethod(data);

        console.log('调用子组件方法');

      }

    }

  };

</script>

子组件:

<template>

  <div>

    <h1>我是子组件</h1>

  </div>

</template>

<script>

  export default {

    methods: {

      childMethod(data) {

        console.log(‘我是子组件方法’)

      }

    }

  };

</script>

子组件调用父组件方法:

1.在子组件中通过this.$parent.event来调用父组件的方法,data参数可选

父组件:

<template>

  <div>   

    <h1>我是父组件</h1>

    <child></child>

  </div>

</template>

<script>

  import child from '~/components/child';

  export default {

    components: {

      child

    },

    methods: {

      fatherMethod(data) {

        console.log('我是父组件方法');

      }

    }

  };

</script>

 子组件:

<template>

  <div>

    <h1>我是子组件</h1>

    <button @click="childMethod(data)">点击</button>

  </div>

</template>

<script>

  export default {

    methods: {

      childMethod() {

        this.$parent.fatherMethod(data);

        console.log('调用父组件方法')

      }

    }

  };

</script>

2.在子组件里用$emit向父组件触发一个事件,父组件监听这个事件,data参数可选

父组件:

<template>

  <div>   

    <h1>我是父组件</h1>

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

  </div>

</template>

<script>

  import child from '~/components/child';

  export default {

    components: {

      child

    },

    methods: {

      fatherMethod(data) {

        console.log('我是父组件方法');

      }

    }

  };

</script>

子组件:

<template>

  <div>

    <h1>我是子组件</h1>

    <button @click="childMethod(data)">点击</button>

  </div>

</template>

<script>

  export default {

    methods: {

      childMethod(data) {

      this.$emit('fatherMethod',data);

        console.log('调用父组件方法')

      }

    }

  };

</script>

3.父组件通过props把方法传入子组件中,在子组件里直接调用这个方法,data参数可选

父组件:

<template>

  <div>   

    <h1>我是父组件</h1>

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

  </div>

</template>

<script>

  import child from '~/components/child';

  export default {

    components: {

      child

    },

    methods: {

      fatherMethod(data) {

        console.log('我是父组件方法');

      }

    }

  };

</script>

子组件:

<template>

  <div>

    <h1>我是子组件</h1>

    <button @click="childMethod(data)">点击</button>

  </div>

</template>

<script>

  export default {

    props: {

      fatherMethod: {

        type: Function,

        default: null

      }

    },

    methods: {

      childMethod(data) {

        if (this.fatherMethod) {

          this.fatherMethod(data);

      console.log('调用父组件传递的方法')

        } 

      }

    }

  };

</script>

相关文章

  • Vue子父组件方法互调

    讲干货,不啰嗦,大家在做vue开发过程中经常遇到父组件需要调用子组件方法或者子组件需要调用父组件的方法的情况,现做...

  • vue.js 核心知识点三

    目录 - 3.1 vue中子组件调用父组件的方法 - 3.2 Vue父组件调用子组件的方法 - 3.3 涉及到组件...

  • vue 父组件调用子组件的方法

    父组件可以通过this.$refs调用子组件的方法 父级.vue 子级 child.vue

  • vue的父子传值和使用vuex兄弟传值

    在父组件中引入子组件 父传子 父组件 子组件 子传父 子组件 父组件 兄弟传值 兄弟之间可以通过vue官网的方法,...

  • Vue父子组件传值

    父传子 父组件 Father.vue 子组件 Son.vue 子传父 子组件 Son.vue 父组件 ...

  • vue父子组件的传值与方法互调

    一、传值 1.父传子 2.子传父 二、父子组件方法互调 1.父调子 2.子调父

  • 2018-04-12 vue父子组件间的通信

    vue + iview 关于父组件模态框内嵌子组件点击确认,执行父组件相应的方法 子组件 this.$emit('...

  • 关于$refs的用法

    $refs父组件调用子组件的方法,可以传递数据。 示例 父组件 $refsFa.vue 子组件 emitCh.vu...

  • Vue父组件调用子组件事件

    Vue父组件向子组件传递事件/调用事件 不是传递数据(props)哦,适用于 Vue 2.0 方法一:子组件监听父...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

网友评论

    本文标题:Vue子父组件方法互调

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