Vue3常规登录页面模板

本文基于vue3(JavaScript),使用vue3的setup语法糖书写方式

setup语法糖也是当前各大适用Vue的框架官网都在推崇的书写方式,此外各大主流框架的源码首选是TypeScript,而不是JavaScript。

登录页面模板

以下登录页面模板,基于element-plus和scss,关于elment-plus和scss的环境准备本文不做赘述,包括element-plus图标库的引入。

效果图:
在这里插入图片描述

<template>
    <div class="login-body">
        <div class="login-panel">
            <div class="login-title">用户登录</div>
            <el-form :model="formData" :rules="rules" ref="formDataRef">
                <el-form-item prop="username">
                    <el-input placeholder="请输入账号" v-model="formData.username" size="large" type="text">
                        <template #prefix>
                            <el-icon>
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" v-model="formData.password" size="large" type="password"
                        @keyup.enter.native="login()">
                        <template #prefix>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <!-- <el-form-item label="">
                    <div class="check-code-panel">
                        <el-input placeholder="请输入验证码" v-model="formData.checkCode" class="input-panel" />
                        <img src="checkCodeUrl" class="check-code">
                    </div>
                </el-form-item> -->
                <!-- <el-form-item label="">
                    <el-checkbox label="记住密码" name="type" />
                </el-form-item> -->
                <el-form-item label="">
                    <el-button type="primary" style="width: 100%;" @click="login()" size="large">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, } from 'vue'
import { ElMessage } from 'element-plus';
import request from '@/utils/request';		//这里使用自行封装的axios,下文已给出,照搬后修改运行端口即可
import { useRouter } from 'vue-router';

// const checkCodeUrl = "api/checkCode?" + new Date().getTime();
//表单
const formDataRef = ref();
let formData = reactive({
    username: "",
    password: ""
});
const rules = {
    username: [{
        required: true,
        message: "请输入用户名"
    }],
    password: [{
        required: true,
        message: "请输入密码"
    }],
    // checkCode: [{
    //     required: true,
    //     message: "请输入验证码"
    // }],
}

const router = useRouter();

const login = () => {
    var form_obj = JSON.parse(JSON.stringify(formData));
    // console.log(form_obj);
    // console.log(form_obj.username);
    // console.log(form_obj.password);

    // 后端代码自行准备
    request.post("/user/login", form_obj).then(res => {
        if (res) {
            ElMessage({
                message: '登录成功',
                type: 'success',
            })
            
            let tokenObj = { token: " isLogin", startTime: new Date().getTime() };
            window.localStorage.setItem("isLogin", JSON.stringify(tokenObj));
            localStorage.setItem("username", JSON.parse(JSON.stringify(formData.username)));

            router.push("/");

        } else {
            ElMessage.error('账号或密码错误!!!登录失败!!!')
        }
    });
};
</script>

<style lang="scss" scoped >
.login-body {
    background: url("../assets/三门峡.png") no-repeat center center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: absolute;
    left: 0;
    top: 0;

    .login-panel {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;

        padding: 25px;
        width: 26%;
        min-width: 460px;
        height: 30%;
        min-height: 300px;
        background: rgba(255, 255, 255, 0.6);
        border-radius: 5%;
        box-shadow: 2px 2px 10px #ddd;

        .login-title {
            font-size: 22px;
            text-align: center;
            margin-bottom: 22px;
        }
    }
}
</style>

axios封装

utils/request.js

import axios from 'axios'

//这里的端口是后端程序的运行端口
const request = axios.create({
    baseURL: 'http://localhost:10029',  // 注意!! 这里是全局统一加上了 '/api' 前缀,也就是说所有接口都会加上'/api'前缀在,页面里面写接口的时候就不要加 '/api'了,否则会出现2个'/api',类似 '/api/api/user'这样的报错,切记!!!
    timeout: 5000
})
// request 拦截器
// 可以自请求发送前对请求做一些处理
// 比如统一加token,对请求参数统一加密
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';

    // config.headers['token'] = user.token;  // 设置请求头
    return config
}, error => {
    return Promise.reject(error)
});

// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(
    response => {
        let res = response.data;
        // 如果是返回的文件
        if (response.config.responseType === 'blob') {
            return res
        }
        // 兼容服务端返回的字符串数据
        if (typeof res === 'string') {
            res = res ? JSON.parse(res) : res
        }
        return res;
    },
    error => {
        console.log('' + error) // for debug
        return Promise.reject(error)
    }
)

export default request

登录状态定时检测(全局路由守卫)

通过添加路由守卫的方式,监控登录状态,登录成功记录时间戳到本地缓存,若本地缓存的时间戳超过设定的时间则自动退出登录状态。

router/index.js 添加全局路由守卫

import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import { h } from 'vue';

const routes = [
  //登录页路由
  {
    path: '/login',
    name: '登录页',
    component: () => import('../views/LoginView.vue')
  },
  // 页面找不到404 路由
  {
    path: '/404',
    name: 'NoPage 404',
    component: () => import('../views/404.vue'),
    hidden: true
  },
  { 
    path: '/:pathMatch(.*)',
    redirect: '/404',
    hidden: true
  },
  //框架布局路由
  {
    path: '/',
    name: "框架页",
    component: () => import('../views/LayoutView.vue'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: '首页',
        component: () => import('../components/Home.vue')
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


// 设置登录过期时间(一天)86400000 
let expire = 21600000;

//路由守卫    
//全局守卫,登录拦截
//进行路由拦截:当没有登陆标识,直接打回登陆页面,如何避免退回到 登陆页呢?
router.beforeEach((to, from, next) => {
  // 从本地缓存中获取保存的 token 信息
  const tokenObj = JSON.parse(window.localStorage.getItem('isLogin'))
  if (to.path === "/login") {
    next()
  } else {
    // 如果没有token,强制跳转到登录页面;如果有,则判断token时间是否过期
    if (!tokenObj || !tokenObj.token) {
      next('/login')
    } else {
      let date = new Date().getTime();
      // 当前时间 - token中的登录时间 > 设置的过期时间,为过期;则清除token,并强制跳转至登录页
      // 反之为有效期,则放行
      if (date - tokenObj.startTime > expire) {
        window.localStorage.removeItem('isLogin');
        next('/login')
        ElMessage({
          message: h('p', null, [
            h('span', null, '登录状态过期'),
            h('i', { style: 'color: teal' }, '请重新登录!'),
          ]),
        })
      } else {
        next();
      }
    }
  }
});

export default router
  • 10
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3是前端开发中常用的一种JavaScript框架,用于构建用户界面。登录页面模板是一种用于展示登录页面的可重用组件,可以在Vue3中使用。 在Vue3中,我们可以使用Vue CLI(Vue命令行界面)来快速创建一个Vue项目。安装好Vue CLI后,可以运行以下命令创建一个新项目: ``` vue create login-template ``` 然后进入项目目录: ``` cd login-template ``` 在项目中,我们可以创建一个新的组件来表示登录页面。在src目录下创建一个Login.vue文件,然后在这个文件中编写登录页面的HTML和CSS代码,如下所示: ```vue <template> <div class="login"> <h1>Login Page</h1> <form> <label for="username">Username:</label> <input type="text" id="username" v-model="username"> <label for="password">Password:</label> <input type="password" id="password" v-model="password"> <button @click="login">Login</button> </form> </div> </template> <script> export default { data() { return { username: '', password: '' } }, methods: { login() { // 在这里处理登录逻辑,比如发送登录请求到后端API // 根据返回结果决定是否跳转到其他页面或显示错误信息 } } } </script> <style scoped> .login { display: flex; flex-direction: column; align-items: center; margin-top: 100px; } h1 { margin-bottom: 20px; } label { margin-bottom: 10px; } input { margin-bottom: 20px; } button { padding: 10px 20px; } </style> ``` 在这个组件中,我们使用了Vue的单文件组件语法。其中,`template`部分包含了登录页面的HTML结构,`script`部分包含了与登录相关的数据和方法,`style`部分包含了登录页面的样式。 然后,在App.vue中引入并使用登录页面组件,在src目录下的App.vue文件中添加以下代码: ```vue <template> <div id="app"> <Login /> </div> </template> <script> import Login from './Login.vue' export default { name: 'App', components: { Login } } </script> <style> #app { font-family: Arial, Helvetica, sans-serif; } </style> ``` 最后,在main.js文件中创建Vue实例,并将App组件渲染到页面中,如下所示: ```javascript import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app') ``` 至此,我们完成了一个简单的Vue3登录页面模板。在浏览器中运行项目,就可以看到一个基本的登录页面,包含用户名、密码输入框和登录按钮。用户在输入用户名和密码后,可以点击登录按钮触发登录方法。你可以根据自己的需求进一步完善登录逻辑,比如校验用户名密码、跳转到其他页面等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值