请你用vue3给我写一个具有登陆功能的登陆页

时间: 2023-11-27 15:52:08 浏览: 27
好的,以下是一个简单的登陆页,使用了Vue3和Vue Router实现路由跳转和页面渲染,使用了Vuex进行状态管理,使用了axios进行数据请求。 首先,我们需要安装必要的依赖: ``` npm install vue@next vue-router@4 vuex@4 axios ``` 接着,在src目录下新建一个components文件夹,用于存放组件。 在components文件夹下新建一个Login.vue组件,代码如下: ```html <template> <div class="login-container"> <form class="login-form" @submit.prevent="login"> <h2>Login</h2> <div class="form-group"> <label for="username">Username:</label> <input type="text" id="username" v-model="username" required> </div> <div class="form-group"> <label for="password">Password:</label> <input type="password" id="password" v-model="password" required> </div> <button type="submit">Login</button> <div class="error-message" v-if="errorMessage">{{ errorMessage }}</div> </form> </div> </template> <script> import { mapActions } from 'vuex' export default { name: 'Login', data() { return { username: '', password: '', errorMessage: '' } }, methods: { ...mapActions(['loginUser']), async login() { try { await this.loginUser({ username: this.username, password: this.password }) this.$router.push('/') } catch (error) { this.errorMessage = error.response.data.message } } } } </script> <style> .login-container { display: flex; justify-content: center; align-items: center; height: 100vh; } .login-form { display: flex; flex-direction: column; align-items: center; padding: 20px; border: 1px solid #ccc; border-radius: 5px; } .form-group { display: flex; flex-direction: column; margin-bottom: 10px; } label { margin-bottom: 5px; } input { padding: 5px; border-radius: 5px; border: 1px solid #ccc; } button { padding: 5px; border-radius: 5px; background-color: #007bff; color: #fff; border: none; cursor: pointer; } .error-message { color: red; margin-top: 10px; } </style> ``` 这个组件包含一个表单,用户可以输入用户名和密码进行登陆。如果登陆成功,跳转到首页;如果登陆失败,显示错误信息。 接着,在src目录下新建一个store文件夹,用于存放Vuex相关的代码。 在store文件夹下新建一个index.js文件,代码如下: ```javascript import { createStore } from 'vuex' import axios from 'axios' const store = createStore({ state: { user: null }, mutations: { SET_USER(state, user) { state.user = user } }, actions: { async loginUser({ commit }, { username, password }) { const response = await axios.post('/api/login', { username, password }) const user = response.data commit('SET_USER', user) } } }) export default store ``` 这个Vuex store包含一个user状态,用于存放当前用户信息。同时定义了一个loginUser action,用于进行登陆操作。 接着,在src目录下新建一个router.js文件,代码如下: ```javascript import { createRouter, createWebHistory } from 'vue-router' import Home from './components/Home.vue' import Login from './components/Login.vue' const routes = [ { path: '/', component: Home }, { path: '/login', component: Login } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 这个路由配置包含两个路由:首页和登陆页。 接着,在src目录下新建一个main.js文件,代码如下: ```javascript import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store' createApp(App).use(router).use(store).mount('#app') ``` 这个文件创建了一个Vue应用,并将路由和Vuex store挂载到应用中。 最后,在根目录下新建一个server.js文件,用于模拟后端API请求,代码如下: ```javascript const express = require('express') const bodyParser = require('body-parser') const app = express() app.use(bodyParser.json()) app.post('/api/login', (req, res) => { const { username, password } = req.body if (username === 'admin' && password === 'password') { res.json({ id: 1, username: 'admin' }) } else { res.status(401).json({ message: 'Invalid username or password' }) } }) app.listen(3000, () => { console.log('Server is running on http://localhost:3000') }) ``` 这个文件使用Express框架创建了一个简单的API,用于处理登陆请求。如果用户名和密码正确,返回用户信息;否则返回401错误。 现在,我们可以在命令行中运行以下命令启动应用: ``` node server.js ``` 然后,在浏览器中打开http://localhost:3000,即可看到登陆页。 输入正确的用户名和密码(admin/password),即可登陆成功,跳转到首页。 这就是一个简单的具有登陆功能的登陆页,希望对你有所帮助!

相关推荐

最新推荐

recommend-type

解决vue多个路由共用一个页面的问题

下面小编就为大家分享一篇解决vue多个路由共用一个页面的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

vue写h5页面的方法总结

下面就是小编带给大家的如何用vue写h5页面方法操作,希望能够给你们带来一定的帮助,谢谢大家的观看。 1、当拿到设计师给的UI设计图,前端的首要任务就是布局和样式,相信这对于大部分前端工程师来说已经不是什么...
recommend-type

在vue项目实现一个ctrl+f的搜索功能

刚刚接到领导通知,需要实现搜索功能,因为项目是vue的而且是手机端,对我来说有点小难度。经过小编的一番思索最终还是解决了,今天小编把实现过程分享到脚本之家平台,需要的朋友参考下
recommend-type

基于vue写一个全局Message组件的实现

主要介绍了基于vue写一个全局Message组件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Vue拖拽组件列表实现动态页面配置功能

主要介绍了Vue拖拽组件列表实现动态页面配置功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。