当前位置:网站首页>Vue+MySQL实现登录注册案例
Vue+MySQL实现登录注册案例
2022-06-27 19:53:00 【喵喵喵更多】
Vue+MySQL实现登录注册案例
1.新建vue项目并连接数据库
具体步骤见vue连接mysql数据库
2.新建登录页面、注册页面和首页
在src/views文件夹下,新建 login.vue(登录页面)、register.vue(注册页面) 和 home.vue(首页)
根据自己的喜好搭建页面(本人此处使用了elementUI的组件,cv前要先安装elementUI中间件)
npm i element-ui -S
登录页面


4.页面路由配置
在src/router/index.js中配置页面对应路由
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const home = () => import("../views/home.vue") //懒加载
const login = () => import("../views/login.vue")
const register = () => import("../views/register.vue")
const routes = [
{
path: '',
redirect: '/login' //重定向
},
{
path: '/login',
name: 'login',
component: login
},
{
path: '/register',
name: 'register',
component: register
},
{
path: '/home',
name: 'home',
component: home,
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
//解决相同路径跳转报错问题
//使用push的方法
const RouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
return RouterPush.call(this, to).catch(err => err)
}
//使用replace的方法
const RouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (to) {
return RouterReplace.call(this, to).catch(err => err)
}
export default router
4.新建/server/API/login.js
接收 req.query / req.bosy 传递来的参数,通过查询语句查询对应数据并放回结果
let db = require('../db/index')
exports.login = (req, res) => {
var sql = 'select * from user where name = ? and password = ?'
db.query(sql, [req.query.name, req.query.password], (err, data) => {
if(err) {
return res.send({
status: 400,
message: "登录失败"
})
}
if(data.length > 0) {
res.send({
status: 200,
message: "登录成功"
})
}else{
res.send({
status: 202,
message: '用户名或密码错误'
})
}
})
}
exports.register = (req, res) => {
const sql1 = 'select * from user where name = ?'
const sql2 = 'insert into user (name, password) value (?, ?)'
db.query(sql1, [req.body.params.name], (err, data) => {
if(err) {
return res.send({
status: 400,
message: "操作失败"
})
}
if(data.length > 0) {
return res.send({
status: 202,
message: '用户名已存在'
})
}else{
db.query(sql2, [req.body.params.name, req.body.params.password], (err, data) => {
if(err) {
return res.send({
status: 400,
message: "注册失败"
})
}
res.send({
status: 200,
message: "注册成功"
})
})
}
})
}
5.在/server/router.js中配置对应路由
let express = require('express')
let router = express.Router()
let login = require('./API/login')
router.get('/login', login.login)
router.post('/register', login.register)
module.exports = router
6.在/views/login.vue、/views/register.vue和/views/home.vue中编写相应方法
<template>
<div class="bg">
<div id="login">
<h2>登录页面</h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label="用户名:">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="密 码:">
<el-input v-model="form.password" type="password"></el-input>
</el-form-item>
</el-form>
<el-button type="primary" round @click="login" class="btn">登录</el-button>
</div>
</div>
</template>
<script>
import axios from "axios"
export default {
data () {
return {
form: {
username: '',
password: ''
}
};
},
methods: {
login() {
if(this.form.username == '') {
this.$message.error('用户名不能为空');
}else if(this.form.password == '') {
this.$message.error('密码不能为空');
}else{
axios.get('http://127.0.0.1/login', {
params: {
name: this.form.username,
password: this.form.password
}
}).then(res=>{
if(res.data.status == 200) {
this.$router.push({
path: '/home',
query: {
name: this.form.username
}
})
}else{
this.$alert('用户名或密码错误', '登录失败', {
confirmButtonText: '确定',
callback: action => {
this.form.username = '',
this.form.password = ''
}
});
}
}).catch(err=>{
console.log("登录失败" + err);
})
}
},
register() {
this.$router.push('/register')
}
}
}
</script>
<template>
<div class="bg">
<div id="register">
<h2>注册页面</h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label="用户名:">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="密 码:">
<el-input v-model="form.password" type="password"></el-input>
</el-form-item>
</el-form>
<el-button type="primary" round @click="register" class="btn">注册</el-button>
</div>
</div>
</template>
<script>
import axios from "axios"
export default {
data () {
return {
form: {
username: '',
password: ''
},
isnull: false
};
},
methods: {
register() {
if(this.form.username == '') {
this.$message.error('用户名不能为空');
}else if(this.form.password == '') {
this.$message.error('密码不能为空');
}else{
axios.post('http://127.0.0.1/register', {
params: {
name: this.form.username,
password: this.form.password
}
}).then(res => {
// console.log(res.data.message);
if(res.data.status == 200) {
this.$alert('是否返回登录页面', '注册成功', {
confirmButtonText: '确定',
callback: action => {
this.$router.push('/login')
}
})
}else if(res.data.status == 202) {
this.$alert('用户名已存在', '注册失败', {
confirmButtonText: '确定',
callback: action => {
this.form.username = '',
this.form.password = ''
}
})
}else{
console.log(res.message);
}
}).catch(err => {
console.log('操作失败' + err);
})
}
}
}
}
</script>
<template>
<div id="main">
<el-container>
<el-header>
<div class="logo" >
<img src="../assets/img/logo.png"> <!-- 此处请提前准备好图片 -->
</div>
<div class="user">
{
{username}}
</div>
</el-header>
<el-main>main</el-main>
<el-footer>Footer</el-footer>
</el-container>
</div>
</template>
<script>
export default {
name: 'Main',
data() {
return{
username: ''
}
},
created() { //页面创建时,把路由传递来的用户名赋值给data中的username,这样就可以在页面显示用户名了(效果见首页的右上角)
this.username = this.$route.query.name;
}
}
</script>
效果展示
登录注册demo
git源码地址:https://gitee.com/xie-xiaochun/login-registration-demo
注意:资源中不包含数据库,需自己创建数据库,并修改源码中数据库的相关信息。
边栏推荐
- Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
- Typescript learning
- MONTHS_ Between function use
- Transformation from student to engineer
- AQS SOS AQS with me
- Is flush stock trading software reliable?? Is it safe?
- Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
- Management system itclub (medium)
- Golang uses regularity to match substring functions
- Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
猜你喜欢

7 jours d'apprentissage de la programmation simultanée go 7 jours de programmation simultanée go Language Atomic Atomic Atomic actual Operation contains ABA Problems

Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移

管理系统-ITclub(上)

VMware virtual machine PE startup

Codeforces Round #717 (Div. 2)

Experience sharing of meituan 20K Software Test Engineers

對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華

PCIe knowledge point -008: structure of PCIe switch

Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string

Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
随机推荐
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
How many ways does selenium upload files? I don't believe you have me all!
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version
Acwing weekly contest 57- digital operation - (thinking + decomposition of prime factor)
QT base64 encryption and decryption
不外泄的测试用例设计秘籍--模块测试
Crontab scheduled task common commands
Introduction to ARCS Model
Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?
使用Fiddler模拟弱网测试(2G/3G)
Is flush stock trading software reliable?? Is it safe?
對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華
The create database of gbase 8A takes a long time to query and is suspected to be stuck
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题
CDH集群之YARN性能调优
Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~