当前位置:网站首页>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
注意:资源中不包含数据库,需自己创建数据库,并修改源码中数据库的相关信息。
边栏推荐
- Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
- Is flush stock trading software reliable?? Is it safe?
- AQS SOS AQS with me
- Ellipsis after SQLite3 statement Solutions for
- \w和[A-Za-z0-9_],\d和[0-9]等价吗?
- 01 golang environment construction
- Go from introduction to practice -- definition and implementation of behavior (notes)
- Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
- How to open an account for agricultural futures? How much is the handling charge for opening an account for futures? Who can give you a preferential handling charge?
- Use Fiddler to simulate weak network test (2g/3g)
猜你喜欢

AQS SOS AQS with me
How to design an elegant caching function

《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题

Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem

Codeforces Round #716 (Div. 2)

CUDA error:out of memory caused by insufficient video memory of 6G graphics card

解决本地连接不上虚拟机的问题

Windwos 8.1系统安装vmware tool插件报错的解决方法

Learn to go concurrent programming in 7 days go language sync Application and implementation of cond

对话乔心昱:用户是魏牌的产品经理,零焦虑定义豪华
随机推荐
管理系统-ITclub(上)
MONTHS_ Between function use
PCIe knowledge point -008: structure of PCIe switch
How to prioritize the contents in the queue every second
The "business and Application Security Development Forum" held by the ICT Institute was re recognized for the security capability of Tianyi cloud
This set of steps for performance testing using JMeter includes two salary increases and one promotion
年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数
About the SQL injection of davwa, errors are reported: analysis and verification of the causes of legal mix of settlements for operation 'Union'
Go from introduction to practice -- definition and implementation of behavior (notes)
Do280openshift access control -- Security Policy and chapter experiment
How to design an elegant caching function
Hash table - sum of arrays
gomock mockgen : unknown embedded interface
How to do function test well? Are you sure you don't want to know?
Crontab scheduled task common commands
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
Summary of gbase 8A database user password security related parameters
Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
How to open an account for agricultural futures? How much is the handling charge for opening an account for futures? Who can give you a preferential handling charge?
Learn to go concurrent programming in 7 days go language sync Application and implementation of cond