import { ref } from 'vue'
const login = ref('')
const password = ref('')
login.value = 'johndoe'
<template>
<form>
<input v-model="login" type="text" placeholder="Login" />
<input v-model="password" type="password" placeholder="Password" />
<button type="submit">Submit</button>
</form>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const login = ref('')
const password = ref('')
return { login, password };
}
};
</script>
// useForm.js
import { ref } from 'vue';
export function useForm() {
const login = ref('')
const password = ref('')
function submitForm() {
console.log(`User ${login} is logging in`)
}
return { login, password, submitForm };
};
<!-- MyComponent.vue -->
<template>
<form @submit.prevent="submitForm">
<input v-model="login" type="text" placeholder="Login" />
<input v-model="password" type="password" placeholder="Password" />
<button type="submit">Submit</button>
</form>
</template>
<script>
import { useForm } from './useForm.js';
export default {
setup() {
const { login, password, submitForm } = useForm()
return { login, password, submitForm };
}
};
</script>
export const formMixin = {
data() {
login: '',
password: ''
},
methods: {
submitForm() {
console.log(`User ${login} is logging in`)
}
}
}
export const formMixin = {
data() {
login: '',
password: ''
},
methods: {
submitForm() {
console.log(
formId,
`User ${login} is logging in`
)
}
}
}
<template
<form-wrapper>
<template #default="{ login, password, submitForm }">
<form @submit.prevent="submitForm">
<input v-model="login" type="text" placeholder="Login" />
<input v-model="password" type="password" placeholder="Password" />
<button type="submit">Submit</button>
</form>
</template>
</form-wrapper>
</template>
<template
<form-wrapper>
<template #default="{ login, password, submitForm }">
<form @submit.prevent="submitForm">
<input v-model="login" type="text" placeholder="Login" />
<input v-model="password" type="password" placeholder="Password" />
<button type="submit">Submit</button>
</form>
</template>
</form-wrapper>
<div>
{{ login }}
</div>
</template>