Subscribe on changes!

Provide returns wrong 'this' - Should return parent component instead of child.

avatar
Aug 4th 2023

Vue version

3.3.4

Link to minimal reproduction

https://play.vuejs.org/#eNqVVE1v2zAM/SuEL26BxA7Wnbyk6Db00B22Yett3sGxmESpLBkSnaQI8t9HyU7i5gNYAR0kkY+PfBS1jT7XdbJqMMqiMWFVq4LwPtcA46+mqo1GTSmfx2nPyEdXWlkT7yU7WYKDM8ysqSBOUn/jA8e5VkjgSlMjTEA3SuUaNwElcFY0imDrCct9CJfBthdwN/BWUVCRwc0tTO7hJvgDVOhcMccM4idwC9MoAVMEIR0n+oqCmQF2twFeWuTcBeM77D4fWkgX/IJbbc1KCo7YeQmszBEDYJEaqwMo6dhbUwsHWBTlywVAYDtBBNZc8xqnnZzRIGr1HFZFnSyd0dyWECvvDC6PDsnlEcvrz3m0IKpdlqal0AwTqOTKJhop1XWVPrBbahtNssKhMNXDXXKXfExZJ+pfJ+iq4dSatUPLQfKoKynQpHy5Qju0qAVatP9LewLrU5+Yzuj3+rAoxPrpmZyfSOJfjFRof9QkjX4rTaGUWX8Ld2QbPNRSLrB8uXC/dJu2pp8WQ2a9+qmwc6TW/Pj7O254fzBWRjSqa8MV4y90RjU+x9btS6MFp93zC9k+hQ5LPX92jxtC7fZF+USPbzT0w4/HtdKP6bLaPRX3E3k+6kKuwgZgu22f/C48zzF3qrV4F3D0qnDCVTGB1Bl8GNUbGMHoE8yMpuEa5XxBGUyNEnnUBXzmUQFeRRiN7DzqgbgdnbfEV7+dSx+I1Essmf9P7GuIBxD7kPHf9/wf2tClP+RkTHf/AOgLuio=

Steps to reproduce

Run the application.

What is expected?

The text on top 'I should not be displayed' should be replaced by 'I should be displayed'.

What is actually happening?

It shows the child's 'this' scope which returns 'I should not be displayed'

System Info

System:
    OS: macOS 13.5
    CPU: (8) arm64 Apple M1
    Memory: 28.84 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.1/bin/npm
    pnpm: 8.6.10 - ~/.nvm/versions/node/v18.16.1/bin/pnpm
  Browsers:
    Chrome: 115.0.5790.170
    Safari: 16.6

Any additional comments?

The documentation says that 'this' should work: https://vuejs.org/guide/components/provide-inject.html#provide

avatar
Aug 6th 2023
<script lang="ts" setup>
import { ref, provide } from 'vue';

import Comp from './Comp.vue';

const msg = ref('Text');

provide('msg', msg.value);
</script>

<template>
  <Comp />
</template>
<script lang="ts" setup>
import { inject } from 'vue';

const msg = inject('msg');
</script>

<template>
  <div>{{ msg }}</div>
</template>