在 Web 开发中,表单是必不可少的元素,用于收集用户输入。Vuetify 是一个流行的 Vue.js UI 框架,提供了强大的组件库,其中包括表单组件。本文将介绍如何使用 Vuetify 创建一个响应式表单,该表单可在各种设备上良好显示。
此代码创建了一个响应式表单,包括以下功能:
npm install vuetify
<template>
<div class="bg-white dark:bg-gray-900">
<div class="flex justify-center items-center h-screen">
<div class="w-full max-w-md p-4">
<div class="bg-white dark:bg-gray-800 shadow-lg rounded-lg p-8">
<div class="flex justify-center items-center">
<img
src="https://source.unsplash.com/random/300x300"
alt=""
class="w-20 h-20 rounded-full"
/>
</div>
<h2
class="text-2xl font-bold text-center text-gray-700 dark:text-white"
>
learn up
</h2>
<p class="text-center text-gray-600 dark:text-gray-400">
Find the best course for you!
</p>
<div class="mt-4">
<form action="#">
<label
for="name"
class="block mb-2 text-sm font-medium text-gray-700 dark:text-gray-300"
>Name</label
>
<input
type="text"
id="name"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white"
placeholder="Your name"
required
/>
<label
for="email"
class="block mb-2 text-sm font-medium text-gray-700 dark:text-gray-300"
>Email</label
>
<input
type="email"
id="email"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white"
placeholder="Your email"
required
/>
<label
for="password"
class="block mb-2 text-sm font-medium text-gray-700 dark:text-gray-300"
>Password</label
>
<input
type="password"
id="password"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white"
placeholder="Your password"
required
/>
<button
type="submit"
class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
>
Next
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</template>
<script lang="tsx" setup>
import { ref } from 'vue'
const name = ref('')
const email = ref('')
const password = ref('')
const handleSubmit = () => {
console.log(name.value, email.value, password.value)
}
</script>
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
<div class="w-full max-w-md p-4">
<div class="bg-white dark:bg-gray-800 shadow-lg rounded-lg p-8">
<!-- 表单内容 -->
</div>
</div>
此代码使用 max-w-md
类来限制表单的最大宽度,并使用 w-full
类使其在所有可用空间中展开。
<input
type="text"
id="name"
class="..."
required
/>
required
属性可确保在提交表单之前已填写该字段。Vuetify 还提供了其他验证规则,例如 min
、max
和 email
。
<div class="error-message">
Please enter a valid email address.
</div>
当用户输入无效数据时,Vuetify 会自动显示错误消息。您可以通过 v-if
指令显示或隐藏错误消息。
<button
type="submit"
@click="handleSubmit"
>
Next
</button>
const handleSubmit = () => {
console.log(name.value, email.value, password.value)
}
@click
事件侦听器调用 handleSubmit
方法,该方法记录用户输入的数据。
开发这段代码让我学到了如何使用 Vuetify 创建响应式且可验证的表单。我计划在未来扩展此代码,添加以下功能: