需求实战一
效果展示

代码展示
<template>
<div style="display: flex;">
<div style="display: flex; justify-content: center; align-items: center;">
<label for="input" style="font-family: Arial; font-size: 20px; font-weight: bold;">姓名:label>
div>
 
 
 
<div>
<a-input id="input" v-model:value="value" placeholder="请输入姓名" class="custom-input" />
div>
div>
template>
<script setup lang="ts">
const value = ref<string>('');
script>
<style scoped>
:deep(.custom-input) {
border: 1px solid gray;
width:250px;
border-radius: 10px;
padding: 8px;
}
:deep(label) {
margin-right: 10px;
}
style>
代码解释
这段代码是一个Vue组件的模板部分,用于创建一个包含姓名输入框的表单。
在模板中,使用了Flex布局来将姓名标签和输入框放在同一行。姓名标签使用了
在脚本部分,使用了ref函数创建了一个名为value的响应式数据,用于存储输入框的值。
在样式部分,使用了:deep伪类选择器来选择嵌套的元素,并设置了输入框和标签的样式,包括边框、宽度、边框半径和内边距等。
这段代码可以用于在Vue项目中创建一个带有姓名输入框的表单组件。
需求实战二
效果展示

代码展示
<template>
<div>
<div style="display: flex;">
<div style="display: flex; justify-content: center; align-items: center;">
<label for="input" style="font-family: Arial; font-size: 20px; font-weight: bold;">姓名:label>
div>
 
 
 
<div style="display: flex;">
<a-input id="input" v-model:value="name" placeholder="请输入姓名" class="custom-input" :show-word-limit="true" />
 
 
 
<a-alert v-if="name === ''" type="error" message="姓名为必须输入选项" show-icon />
div>
div>
<br>
<br>
<div style="display: flex;">
<div style="display: flex; justify-content: center; align-items: center;">
<label for="input" style="font-family: Arial; font-size: 20px; font-weight: bold;">年龄:label>
div>
 
 
 
<div style="display: flex;">
<a-input id="input" v-model:value="age" placeholder="请输入年龄" class="custom-input" :show-word-limit="true" />
 
 
 
<a-alert v-if="age === ''" type="error" message="年龄为必须输入选项" show-icon />
div>
div>
div>
template>
<script setup lang="ts">
const name = ref<string>('');
const age = ref<string>('');
script>
<style scoped>
:deep(.custom-input) {
border: 1px solid gray;
width:250px;
border-radius: 10px;
padding: 8px;
}
:deep(label) {
margin-right: 10px;
}
style>
代码解释
这段代码是一个Vue组件的模板,用于创建一个包含姓名和年龄输入框的表单。以下是代码的解释:
- 在模板的顶部,我们使用了
元素来包裹整个表单。
- 表单中的每个输入框都被包裹在一个
元素中,并使用display: flex来实现水平排列。
- 每个输入框都由一个
-
- 元素是一个自定义的输入框组件,通过v-model:value指令将输入框的值与Vue实例中的name和age变量进行双向绑定。
- 输入框的样式通过.custom-input类进行自定义,包括边框、宽度、边框半径和内边距。
- 如果输入框的值为空,将显示一个错误提示框,提示用户该字段为必填项。
在