前端学习记录

Vue子父组件方法互调

2019-07-22  本文已影响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>

上一篇 下一篇

猜你喜欢

热点阅读