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
inject
correctly.The Warning basically says what the issue is, you need to use
inject
insidesetup
.You're using
inject
insidelog
, which is not the same function scope.You can't call
provide
orinject
inside a callback function, as that is outside thesetup
context.Instead call the injection inside the setup function and use refs/reactive if you need reactive values from your injection.
Here's a sandbox.