当前位置:首页 > VUE

vue 3.0登录界面实现

2026-02-23 12:26:29VUE

Vue 3.0 登录界面实现步骤

环境准备
确保已安装 Node.js(建议版本 14+)和 Vue CLI(全局安装命令:npm install -g @vue/cli)。通过 vue create project-name 创建 Vue 3 项目,选择 Vue 3 模板。

组件结构
创建 Login.vue 文件,包含表单元素(用户名、密码输入框、提交按钮)。使用 v-model 双向绑定数据,例如:

<template>
  <form @submit.prevent="handleSubmit">
    <input v-model="form.username" placeholder="用户名" />
    <input v-model="form.password" type="password" placeholder="密码" />
    <button type="submit">登录</button>
  </form>
</template>

状态管理
<script setup> 中使用 refreactive 管理表单数据:

<script setup>
import { reactive } from 'vue';
const form = reactive({
  username: '',
  password: ''
});
const handleSubmit = () => {
  console.log('提交数据:', form);
};
</script>

表单验证
引入 vee-validate 或手动验证。示例手动验证:

const validate = () => {
  if (!form.username.trim()) return '用户名不能为空';
  if (form.password.length < 6) return '密码至少6位';
  return null;
};
const handleSubmit = () => {
  const error = validate();
  if (error) alert(error);
  else alert('验证通过');
};

样式设计
使用 <style scoped> 添加局部样式,或引入 UI 库(如 Element Plus、Ant Design Vue)快速构建。Element Plus 示例:

<template>
  <el-form @submit.prevent="handleSubmit">
    <el-form-item label="用户名">
      <el-input v-model="form.username" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="form.password" type="password" />
    </el-form-item>
    <el-button type="primary" native-type="submit">登录</el-button>
  </el-form>
</template>

路由与权限
安装 vue-router,配置登录后跳转:

// router/index.js
import { createRouter } from 'vue-router';
const routes = [
  { path: '/login', component: () => import('@/views/Login.vue') },
  { path: '/home', component: () => import('@/views/Home.vue'), meta: { requiresAuth: true } }
];
const router = createRouter({ routes });

API 请求
通过 axios 发送登录请求,保存 token 至本地存储:

import axios from 'axios';
const handleSubmit = async () => {
  try {
    const res = await axios.post('/api/login', form);
    localStorage.setItem('token', res.data.token);
    router.push('/home');
  } catch (error) {
    alert('登录失败: ' + error.response.data.message);
  }
};

响应式布局
使用 CSS Flex/Grid 或媒体查询适配不同屏幕。例如:

form {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}
@media (max-width: 600px) {
  input { width: 100%; }
}

完整示例代码
整合上述内容后的 Login.vue

<template>
  <el-form @submit.prevent="handleSubmit" class="login-form">
    <el-form-item label="用户名">
      <el-input v-model="form.username" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="form.password" type="password" />
    </el-form-item>
    <el-button type="primary" native-type="submit">登录</el-button>
  </el-form>
</template>

<script setup>
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
const form = reactive({ username: '', password: '' });
const router = useRouter();
const handleSubmit = async () => {
  try {
    const res = await axios.post('/api/login', form);
    localStorage.setItem('token', res.data.token);
    router.push('/home');
  } catch (error) {
    alert('登录失败: ' + error.response.data.message);
  }
};
</script>

<style scoped>
.login-form {
  max-width: 400px;
  margin: 50px auto;
}
</style>

vue 3.0登录界面实现

标签: 界面vue
分享给朋友:

相关文章

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在V…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: da…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导…