vue3+ts+elementplus写一个登录页面教程

在这里插入图片描述


前言

前期准备步骤:
创建一个使用 Vue 3 和 TypeScript 的登录页面涉及到多个步骤。以下是一个基本的教程,帮助你从头开始构建这样一个页面:

1. 安装 Vue CLI 和 TypeScript 支持

首先,你需要安装 Vue CLI 并配置它以支持 TypeScript。如果你还没有安装 Vue CLI,可以通过以下命令进行安装:

npm install -g @vue/cli  
# OR  
yarn global add @vue/cli

然后,你可以创建一个新的 Vue 3 项目并启用 TypeScript 支持:

vue create my-login-app

在创建过程中,选择 “Manually select features”,然后勾选 “TypeScript” 和其他你需要的特性(比如 Router、Linter/Formatter 等)。

2. 创建登录组件

在 src/views目录下创建一个新的 TypeScript 文件,比如 Login.vue。这个文件将包含你的登录表单和逻辑。

文章重点内容

页面样式:
在这里插入图片描述
页面代码:

		<div class="login-right-warp">
				<div class="login-right-warp-title">login</div>
				<div class="login-right-warp-form">
					<div class="login-right-warp-form_title">用户名登录</div>

					<el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" status-icon :rules="rules"
						label-width="auto" class="demo-ruleForm">
						<el-form-item prop="AccountNo">
							<el-input v-model="ruleForm.AccountNo" autocomplete="off" :prefix-icon="User"
								placeholder="请输入账号" />
						</el-form-item>
						<el-form-item prop="AccountPwd">
							<el-input v-model="ruleForm.AccountPwd" type="password" autocomplete="off" :prefix-icon="Lock"
								placeholder="请输入密码" />
						</el-form-item>
						<el-form-item prop="Code">
							<el-input v-model.number="ruleForm.Code" :prefix-icon="Promotion" class="login-content-code"
								placeholder="请输入验证码" />
							<span class="login-content-code-img" @click="generateCodeAsync()">{{ verificationCode.Code
							}}</span>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
						</el-form-item>
					</el-form>
					<div class="login-msg">* 温馨提示:建议使用谷歌、Microsoft Edge,版本 79.0.1072.62 及以上浏览器,360浏览器请使用极速模式</div>
				</div>
			</div>

script代码:

import { reactive, onMounted, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { User, Lock, Promotion } from '@element-plus/icons-vue';
import { postLoginAsync, postGenerateCodeAsync } from '@/api/login';
import { useRouter } from 'vue-router';

// 使用 Vue Router 的 useRouter 函数来获取 router 实例
const router = useRouter();

const ruleFormRef = ref<FormInstance>();

// 页面加载时
onMounted(() => {
	generateCodeAsync();
});

const validateCodeKey = (rule: any, value: any, callback: any) => {
	if (value === '') {
		callback(new Error('请输入验证码'));
	} else {
		callback();
	}
};

const validateAccountNo = (rule: any, value: any, callback: any) => {
	if (value === '') {
		callback(new Error('请输入账号'));
	} else {
		callback();
	}
};
const validateAccountPwd = (rule: any, value: any, callback: any) => {
	if (value === '') {
		callback(new Error('请输入密码'));
	} else {
		callback();
	}
};

const ruleForm = reactive({
	AccountNo: '123',
	AccountPwd: '',
	CodeKey: '',
	Code: '',
});

const verificationCode = reactive({
	CodeKey: '',
	Code: '',
});



const rules = reactive<FormRules<typeof ruleForm>>({
	AccountNo: [{ validator: validateAccountNo, trigger: 'blur' }],
	AccountPwd: [{ validator: validateAccountPwd, trigger: 'blur' }],
	Code: [{ validator: validateCodeKey, trigger: 'blur' }],
});
const generateCodeAsync = () => {
	postGenerateCodeAsync().then((res: any) => {
		verificationCode.Code = res.Data.Code;
		verificationCode.CodeKey = res.Data.Key;
		ruleForm.CodeKey = res.Data.Key;
	});
};

const submitForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid) => {
		if (valid) {
			console.log('submit!');
			router.push('/home');
		} else {
			console.log('error submit!');
			return false;
		}
	});
};
</script>

样式代码:

.login {
	width: 100vw;
	height: 100vh;
	background: #fff;

	.login-left {
		.login-left-logo {
			display: flex;
			align-items: center;
			position: absolute;
			top: 50px;
			left: 80px;
			z-index: 1;
			animation: logoAnimation 0.3s ease;

			img {
				width: 100px;
				height: 64px;
			}

			.login-left-logo-text {
				display: flex;
				flex-direction: column;
			}

			.login-left-logo-text span {
				margin-left: 10px;
				font-size: 30px;
				font-weight: 700;
				color: #de2910;
			}

			.login-left-logo-text .login-left-logo-text-msg {
				font-size: 12px;
				color: #de2910;
			}
		}

		.login-left-img {
			width: 90%;
			margin: 70px 0 0px 10%;
			height: 90%;
			min-height: 500px;
			min-width: 500px;
			display: flex;
			justify-content: center;
			align-items: center;

			img {
				height: 500px;
				min-width: 500px;
			}
		}
	}

	.login-right {
		width: 650px;

		.login-right-warp {
			border: 1px solid #e76959;
			border-radius: 3px;
			width: 500px;
			height: 550px;
			position: relative;
			overflow: hidden;
			background-color: #fff;
			margin: auto;

			.login-right-warp-title {
				height: 130px;
				line-height: 130px;
				font-size: 32px;
				font-weight: 800;
				text-align: center;
				animation: logoAnimation 0.3s ease;
				animation-delay: 0.3s;
				color: #f00;
			}

			.login-right-warp-form {
				padding: 0 50px 50px;
			}

			.login-right-warp-form_title {
				color: #de2910;
				width: 100%;
				padding: 10px 0px;
				border-bottom: 1px solid #e4e7ed;
				position: relative;
			}

			.login-right-warp-form_title::after {
				content: '';
				position: absolute;
				width: 90px;
				height: 2px;
				background: #de2910;
				bottom: 0;
				left: 0px;
			}
		}

		.el-form {
			margin-top: 30px;
		}

		.el-input {
			width: 100%;
			height: 40px;
		}

		.el-button {
			width: 100%;
			height: 40px;
			border-radius: 20px;
			background: #de2910;
			border: #de2910;
			margin: 20px 0px;
		}

		.login-content-code {
			width: 70%;
		}

		.login-content-code-img {
			width: 25%;
			background-color: #fff;
			border: 1px solid rgb(220, 223, 230);
			height: 38px;
			border-radius: 5px;
			margin-left: 4.5%;
			line-height: 38px;
			text-align: center;
			cursor: pointer;
		}

		.login-msg {
			margin-top: 30px;
			color: #a8abb2;
			font-size: 12px;
		}
	}
}

您好,我是肥晨。
欢迎关注我获取前端学习资源,日常分享技术变革,生存法则;行业内幕,洞察先机。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是关于vue3+ts+elementplus聊天界面的开发方案: 1. 安装Vue CLI并创建Vue3项目 首先,您需要安装Vue CLI来创建Vue3项目。您可以使用以下命令安装Vue CLI: ``` npm install -g @vue/cli ``` 安装完成后,您可以使用以下命令创建Vue3项目: ``` vue create my-project ``` 2. 安装Element Plus Element Plus是一套基于Vue3的组件库,适用于PC端和移动端。您可以使用以下命令安装Element Plus: ``` npm install element-plus --save ``` 3. 集成TypeScript Vue3已经默认支持TypeScript,您可以使用以下命令安装TypeScript: ``` npm install typescript --save-dev ``` 4. 开始开发聊天界面 在开发聊天界面之前,您需要先确定聊天数据的结构。您可以使用以下数据结构: ``` interface Message { id: number; content: string; sender: string; receiver: string; time: string; } ``` 接下来,您可以使用Element Plus中的组件来构建聊天界面。以下是一个简单的聊天界面示例: ``` <template> <div class="chat-window"> <div class="chat-history"> <div v-for="message in messages" :key="message.id" class="message-row"> <div class="message-sender">{{ message.sender }}</div> <div class="message-content">{{ message.content }}</div> <div class="message-time">{{ message.time }}</div> </div> </div> <div class="chat-input"> <el-input v-model="inputMessage" placeholder="请输入消息"></el-input> <el-button @click="sendMessage">发送</el-button> </div> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; interface Message { id: number; content: string; sender: string; receiver: string; time: string; } export default defineComponent({ name: 'ChatWindow', setup() { const messages = ref<Message[]>([ { id: 1, content: '你好', sender: '小明', receiver: '小红', time: '2022-01-01 12:00:00', }, { id: 2, content: '你好,很高兴认识你', sender: '小红', receiver: '小明', time: '2022-01-01 12:01:00', }, ]); const inputMessage = ref(''); const sendMessage = () => { const newMessage: Message = { id: messages.value.length + 1, content: inputMessage.value, sender: '小明', receiver: '小红', time: new Date().toLocaleString(), }; messages.value.push(newMessage); inputMessage.value = ''; }; return { messages, inputMessage, sendMessage, }; }, }); </script> <style> .chat-window { display: flex; flex-direction: column; width: 500px; height: 500px; border: 1px solid #ccc; overflow: hidden; } .chat-history { flex: 1; padding: 10px; overflow-y: auto; } .message-row { display: flex; flex-direction: column; margin-bottom: 10px; } .message-sender { font-weight: bold; margin-bottom: 5px; } .message-content { margin-bottom: 5px; } .message-time { font-size: 12px; color: #999; } .chat-input { display: flex; padding: 10px; background-color: #f0f0f0; } .el-input { flex: 1; margin-right: 10px; } .el-button { width: 80px; } </style> ``` 以上示例中,我们使用了Element Plus中的Input和Button组件来实现发送消息的功能,使用了Vue3中的Composition API来管理组件状态。 希望这个开发方案对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奶糖 肥晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值