【VUE实战问题记录】Vue 子组件调用父组件的使用方法 provide/inject 使用

编写项目的时候,要对代码进行抽离,封装成一个单独的组件,这个时候数据的传输,方法的调用就成了一个很大的问题,那么我们来实现如何在父组件中调用子组件。

第1种方法:provide/inject

在某些场景下,我们的组件嵌套层级会非常深,这个时候如果使用 props 的方式传递祖先组件的方法或属性,并不是一个明智的选择,我们应该使用 provide/inject 来跨层级访问祖先组件的数据

父组件

// Parent.vue
<template>
  <div class="container">
    <h3>provide/inject 传递方法</h3>
    <div class="description">
      在父组件中通过 provide 向子孙组件提供方法,在子孙组件中通过 inject
      获取父组件提供的方法
    </div>
    <br />
    <Child></Child>
  </div>
</template>
<script>
import Child from "./Children";
export default {
  name: "Father",
  components: {
    Child,
  },
  provide() {
    return {
      fatherMethod: this.fatherMethodHandle,
    };
  },
  methods: {
    fatherMethodHandle() {
      console.log("我是父组件的方法");
    },
  },
};
</script>

<style scoped lang="scss">
</style>

子组件

<template>
  <div>
    <button @click="childMethod">我是子组件</button>
  </div>
</template>
<script>
export default {
  name: "Child",
  props: {
    msg: String,
  },
  inject: ["fatherMethod"],
  methods: {
    childMethod() {
      console.log("我是子组件的方法,我在子组件中调用了父组件的方法");
      this.fatherMethod();
    },
  },
};
</script>
<style scoped>
</style>

第2种方法:

在父组件中通过 props 的方式传入子组件中,在子组件中直接调用这个方法。在嵌套层级很深的子组件中不建议使用 props 的方式传递父组件的方法,因为层层传递会导致代码变得难以维护

父组件

// Parent.vue

<template>
  <div class="container">
    <h3>props 方式传递方法</h3>
    <div class="description">
      在父组件中通过 props 的方式 向子孙组件传递方法
    </div>
    <br />
    <Child :fatherMethod="fatherMethodHandle"></Child>
  </div>
</template>

<script>
import Child from "./Child";

export default {
  name: "Father",
  components: {
    Child,
  },
  methods: {
    fatherMethodHandle() {
      console.log("我是父组件的方法");
    },
  },
};
</script>
<style scoped lang="scss">
</style>

子组件

// Child.vue
<template>
  <div>
    <button @click="childMethod">我是子组件</button>
  </div>
</template>

<script>
export default {
  name: "Child",
  props: {
    fatherMethod: {
      type: Function,
      require: true,
      default: null,
    },
  },
  methods: {
    childMethod() {
      console.log(
        "我是子组件的方法,我在子组件中通过 props 的方式调用了父组件的方法"
      );
      this.fatherMethod();
    },
  },
};
</script>
<style scoped>
</style>

第3种方法:

父组件

<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>

第4种方法

父组件

<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>

第5种方法

父组件

<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
文章2

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3中,可以使用`script setup`语法来编写组件。在`script setup`中,可以使用`defineEmits`定义组件可以发出的事件,以及使用`defineExpose`来将组件的状态和方法暴露给组件。 接下来,我们可以在组件使用`provide`来向组件提供一个响应事件的方法,然后在组件使用`inject`来获取这个方法,并在需要的时候进行调用。 下面是一个示例代码: ```vue <template> <div> <p>组件发出的事件:{{ message }}</p> <Child @emit-message="handleMessage" /> </div> </template> <script setup> import { ref, provide } from 'vue' import Child from './Child.vue' // 定义组件可以发出的事件 const emitMessage = defineEmits(['emit-message']) // 定义状态和方法 const message = ref('') // 提供一个响应事件的方法 const handleMessage = (msg) => { message.value = msg } // 将方法提供给组件 provide('handle-message', handleMessage) </script> <script> import Child from './Child.vue' export default { components: { Child }, data() { return { message: '' } }, methods: { // 响应组件发出的事件 handleMessage(msg) { this.message = msg } } } </script> ``` 在上面的代码中,组件提供了一个名为`handle-message`的方法组件可以使用`inject`来获取这个方法并在需要的时候进行调用组件使用了`defineEmits`来定义了一个`emit-message`事件,组件可以通过调用`emit`方法来触发这个事件。在组件中,我们通过`@emit-message="handleMessage"`来监听组件发出的事件,并在事件触发时调用`handleMessage`方法来更新状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值