Provide / Inject #697
-
|
Is it possible to use Provide / Inject with custom nodes? I try to call inject inside custom node but always return undefined with console warn: Example: https://codesandbox.io/p/sandbox/peaceful-kalam-n7pdqk |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You're not using You can't call Instead call the injection inside the setup function and use refs/reactive if you need reactive values from your injection. // App.vue
const message = ref('')
provide(MessageInjection, message)// CustomNode.vue
const message = inject(MessageInjection, ref(''))
function log() {
console.log(message.value)
}
<button @click="log">Log Message</button> |
Beta Was this translation helpful? Give feedback.
You're not using
injectcorrectly.The Warning basically says what the issue is, you need to use
injectinsidesetup.You're using
injectinsidelog, which is not the same function scope.You can't call
provideorinjectinside a callback function, as that is outside thesetupcontext.Instead call the injection inside the setup function and use refs/reactive if you need reactive values from your injection.
Here's a sandbox.