Subscribe on changes!

use callback function in the provide it wasn't work

avatar
Feb 2nd 2021

Version

3.0.5

Reproduction link

https://codesandbox.io/s/quizzical-hooks-1gbju?file=/src/components/Carousel.vue

Steps to reproduce

chidren

<template>
  <div>
    <slot></slot>
  </div>
</template>

<script>
import { inject, onMounted, defineComponent, getCurrentInstance } from "vue";
export default defineComponent({
  name: "ci",
  setup(_props) {
    const instance = getCurrentInstance();
    onMounted(() => {
      const { getChilrenItems } = inject("CAROUSEL");
      getChilrenItems(instance);
    });
    return {};
  },
});
</script>

parent

<template>
  <div>
    <slot></slot>
    <div>{{ items }}</div>
  </div>
</template>

<script>
import { ref, provide, defineComponent } from "vue";
export default defineComponent({
  name: "c",
  setup(_props) {
    const items = ref([]);
    function getChilrenItems(child) {
      items.value.push(child.proxy);
    }
    provide("CAROUSEL", { getChilrenItems });
    return { items };
  },
});
</script>

What is expected?

the variable was't empty in the template

What is actually happening?

[{},{},{},{}]

avatar
Feb 2nd 2021

They are not empty, they are printed as empty. BTW, you should inject outside of onMounted()


Please, next time consider using the forum, the Discord server or StackOverflow for questions first. But feel free to come back and open an issue if it turns out to be a bug 🙂