Form Guardian для Vue
Полное руководство по использованию Form Guardian с Vue 3 и Vue 2 приложениями.
🚀 Быстрый старт
Установка
npm install @form-guardian/dom
Vue 3 Composition API
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { attachFormAutosave } from '@form-guardian/dom';
const formRef = ref(null);
let autosave = null;
onMounted(() => {
autosave = attachFormAutosave({
formId: 'contact-form',
root: formRef.value,
autoRestore: true,
debounceMs: 500,
});
});
onBeforeUnmount(() => {
if (autosave) {
autosave.destroy();
}
});
const handleSubmit = async () => {
await autosave.clear();
// Логика отправки...
};
</script>
<template>
<form ref="formRef" @submit.prevent="handleSubmit">
<input v-model="name" name="name" placeholder="Имя" />
<input v-model="email" name="email" type="email" placeholder="Email" />
<textarea v-model="message" name="message" placeholder="Сообщение" />
<button type="submit">Отправить</button>
</form>
</template>
Vue 2 Options API
<template>
<form ref="contactForm" @submit.prevent="handleSubmit">
<input v-model="form.name" name="name" placeholder="Имя" />
<input v-model="form.email" name="email" type="email" />
<textarea v-model="form.message" name="message" />
<button type="submit">Отправить</button>
</form>
</template>
<script>
import { attachFormAutosave } from '@form-guardian/dom';
export default {
name: 'ContactForm',
data() {
return {
form: {
name: '',
email: '',
message: '',
},
autosave: null,
};
},
mounted() {
this.autosave = attachFormAutosave({
formId: 'contact-form',
root: this.$refs.contactForm,
autoRestore: true,
});
},
beforeUnmount() { // Vue 3: beforeUnmount, Vue 2: beforeDestroy
if (this.autosave) {
this.autosave.destroy();
}
},
methods: {
async handleSubmit() {
await this.autosave.clear();
// Логика отправки...
},
},
};
</script>