Vue实现简单用户登录页面

📑前言

本文主要是【Vue】------Vue实现简单用户登录页面的文章,如果有什么需要改进的地方还请大佬指出⛺️

🎬作者简介:大家好,我是听风与他🥇

☁️博客首页:CSDN主页听风与他

🌄每日一句:狠狠沉淀,顶峰相见

目录

页面要求:

  • 登录页面需要有标题,用来提示用户当前的登录状态
  • 在未登录时,需要有两个输入框和登录按钮供用户输入账号、密码以及进行登录操作。
  • 在登录完成后,输入框需要隐藏,需要提供按钮让用户登出

Vue实现代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue用户登录</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="Application" style="text-align: center;">
        <!-- 标题 -->
      <h1>{{title}}</h1>  
      <!-- 账号和密码输入组件 -->
      <div v-if="noLogin">账号:<input v-model="username" type="text"></div>
      <div v-if="noLogin">密码:<input v-model="password" type="password"></div>
      <div v-on:click="click" style="border-radius: 30px;width: 100px;margin: 20px auto;color: white;
      background-color: blue;">{{buttonTitle}}</div>
    </div>
    <script>
        const App = {
            // 定义页面所需要的数据
            data(){
                return{
                    title:'欢迎您:未登录',
                    noLogin:true,
                    username:'',
                    password:'',
                    buttonTitle:'登录'
                }
            },
            methods:{
                // 单击登录执行按钮执行的方法
                click(){
                    if(this.noLogin){
                        this.login()
                    }else{
                        this.logout()
                    }
                },
                // 定义登录操作所执行的函数
                login(){
                    // 判断账号密码是否为空
                    if(this.username.length > 0 && this.password.length > 0){
                        // 模拟登录操作,进行弹窗提示
                        alert(`userName:${this.username} password:${this.password}`)
                        this.noLogin = false
                        this.title = `欢迎您:${this.username}`
                        this.buttonTitle = '退出'
                        this.username = ''
                        this.password = ''
                    }else{
                        alert('请输入账号密码')
                    }
                },
                // 定义登出操作所执行的函数
                logout(){
                    //清空登录数据
                    this.noLogin = true
                    this.title = '欢迎您,未登录'
                    this.buttonTitle = '登录'
                }
            }
        }
        // vue组件绑定
        Vue.createApp(App).mount("#Application")
    </script>
</body>
</html>

登录页面

登录提示

登录成功页面

点击退出-页面

📑文章末尾

相关推荐
子兮曰1 小时前
async/await高级模式:async迭代器、错误边界与并发控制
前端·javascript·github
恋猫de小郭2 小时前
2026 Flutter VS React Native ,同时在 AI 时代 VS Native 开发,你没见过的版本
android·前端·flutter
GIS之路4 小时前
ArcGIS Pro 中的 Notebooks 入门
前端
IT_陈寒5 小时前
React状态管理终极对决:Redux vs Context API谁更胜一筹?
前端·人工智能·后端
lemon_yyds6 小时前
《vue 2 升级vue3 父组件 子组件 传值: value 和 v-model
vue.js
Kagol6 小时前
TinyVue 支持 Skills 啦!现在你可以让 AI 使用 TinyVue 组件搭建项目
前端·agent·ai编程
柳杉6 小时前
从零打造 AI 全球趋势监测大屏
前端·javascript·aigc
simple_lau6 小时前
Cursor配置MasterGo MCP:一键读取设计稿生成高还原度前端代码
前端·javascript·vue.js
睡不着先生6 小时前
如何设计一个真正可扩展的表单生成器?
前端·javascript·vue.js
天蓝色的鱼鱼6 小时前
模块化与组件化:90%的前端开发者都没搞懂的本质区别
前端·架构·代码规范