Use Provide/Inject with reactive form #13162
Replies: 1 comment
|
Your For example, Parent.vue: <script setup>
import { reactive, provide } from 'vue'
import UserForm from './UserForm.vue'
const userForm = reactive({ name: null, email: null })
provide('user-form-key', userForm)
</script>
<template>
<UserForm />
<p>{{ userForm.name }} / {{ userForm.email }}</p>
</template>UserForm.vue: <script setup>
import { inject } from 'vue'
const userForm = inject('user-form-key')
if (!userForm) throw new Error('UserForm requires a form provider')
</script>
<template>
<label>Name <input v-model="userForm.name" /></label>
<label>Email <input v-model="userForm.email" /></label>
</template>Editing either input updates the parent's displayed values because both components use the same proxy. Keep This allows the child to mutate the provided form. If you want mutations owned by the provider, provide a I compiled and mounted these two components with Vue / compiler-sfc 3.5.14 in jsdom, then dispatched input events and checked that the parent updated. |
Uh oh!
There was an error while loading. Please reload this page.
I need help how i can use reactive with provide and inject
Like this
const userForm = reactive({
name:null,
email:null
});
provide('user-form-key',userForm);
All reactions