在许多应用场景中,需要用户选择其身份才能继续使用系统。例如,医疗保健应用可能需要用户选择自己是医生、医院、药房还是患者。身份选择器组件为用户提供了一个简单的方法来指定他们的身份,从而简化了注册或登录流程。
本代码段实现了一个多身份选择器组件,允许用户从一组预定义的身份中选择一个。组件采用响应式设计,可在各种设备上使用。
<template>
<div
class="flex flex-col items-center justify-center h-screen bg-gradient-to-b from-blue-500 to-blue-300"
>
<div
class="flex flex-col items-center justify-center w-full max-w-md p-4 mx-auto my-auto bg-white rounded-lg border border-gray-200 shadow-md"
>
<!-- 顶部栏 -->
<div class="flex flex-row items-center justify-between w-full">
<div class="flex flex-row items-center justify-start">
<van-icon name="arrow-left" size="24" color="#000000" />
<div class="text-xl font-bold text-gray-800">I'm a</div>
</div>
<div class="flex flex-row items-center justify-end">
<van-icon name="ellipsis" size="24" color="#000000" />
</div>
</div>
<!-- 身份选择列表 -->
<div class="flex flex-col items-center justify-center w-full mt-4">
<div class="flex flex-row items-center justify-center w-full">
<van-radio v-model="identity" :label="'doctor'"
>I'm a Doctor</van-radio
>
</div>
<div class="flex flex-row items-center justify-center w-full mt-4">
<van-radio v-model="identity" :label="'hospital'"
>I'm a Hospital</van-radio
>
</div>
<div class="flex flex-row items-center justify-center w-full mt-4">
<van-radio v-model="identity" :label="'pharmacy'"
>I'm a Pharmacy</van-radio
>
</div>
<div class="flex flex-row items-center justify-center w-full mt-4">
<van-radio v-model="identity" :label="'patient'"
>I'm a Patient</van-radio
>
</div>
<div class="flex flex-row items-center justify-center w-full mt-4">
<van-radio v-model="identity" :label="'clinic'"
>I'm a Clinic</van-radio
>
</div>
<div class="flex flex-row items-center justify-center w-full mt-4">
<van-radio v-model="identity" :label="'laboratory'"
>I'm a Laboratory</van-radio
>
</div>
</div>
<!-- 下一步按钮 -->
<div class="flex flex-row items-center justify-center w-full mt-8">
<van-button round block type="primary" size="large" @click="nextStep"
>NEXT STEP</van-button
>
</div>
</div>
</div>
</template>
组件模板定义了组件的结构和样式。顶部栏包含返回和省略号图标。身份选择列表是一个包含多个单选按钮的列,每个按钮对应一个身份。下一步按钮用于提交用户选择。
<script lang="tsx" setup>
import { ref } from 'vue';
import { Calendar } from 'v-calendar';
import { BMap } from 'vue3-baidu-map-gl';
import * as echarts from 'echarts';
import { h } from "vue";
import { createComponent } from 'echarts-for-vue';
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onBeforeUnmount } from 'vue';
const ECharts = createComponent({echarts, h});
const identity = ref('');
const nextStep = () => {
console.log('nextStep', identity.value);
};
onBeforeUnmount(() => {
// Destroy the editor instance before the component is unmounted
editor.value.destroy();
editor.value = null;
});
</script>
组件脚本使用 Vue.js 的 ref
API 来管理组件状态。identity
变量存储用户选择的身份。nextStep
函数在用户单击下一步按钮时触发,并记录用户选择的身份。onBeforeUnmount
钩子用于在组件卸载时销毁编辑器实例。
<style>
.van-radio__icon {
width: 24px;
height: 24px;
border-radius: 50%;
background-color: #000000;
}
.van-radio__label {
margin-left: 8px;
font-size: 16px;
color: #000000;
}
.van-button {
width: 100%;
height: 44px;
line-height: 44px;
font-size: 16px;
border-radius: 22px;
background-color: #000000;
color: #ffffff;
}
</style>
组件样式定义了单选按钮和下一步按钮的外观。
开发这个多身份选择器组件的过程让我对 Vue.js 的响应式编程和组件化有了更深入的理解。我学会了如何使用 ref
API 管理组件状态,以及如何使用 v-model
实现表单绑定。
未来,该组件可以扩展以支持更多的身份,并可以添加自定义验证以确保用户选择了一个有效的身份。此外,可以探索将组件与其他表单元素集成,以创建更复杂的身份验证流程。