Skip to content

Provide / Inject #697

Answered by bcakmakoglu
azamat7g asked this question in Q&A
Discussion options

You must be logged in to vote

You're not using inject correctly.
The Warning basically says what the issue is, you need to use inject inside setup.
You're using inject inside log, which is not the same function scope.

You can't call provide or inject inside a callback function, as that is outside the setup context.

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>

Here's a sandbox.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by azamat7g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants