当前位置:网站首页>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
注意:资源中不包含数据库,需自己创建数据库,并修改源码中数据库的相关信息。
边栏推荐
- Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
- Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
- 爬虫笔记(3)-selenium和requests
- [MySQL practice] query statement demonstration
- It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
- Go language slice vs array panic: runtime error: index out of range problem solving
- 软件测试自动化测试之——接口测试从入门到精通,每天学习一点点
- Go from introduction to actual combat - execute only once (note)
- . Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
- 扁平数组和JSON树的转换
猜你喜欢

爬虫笔记(1)- urllib

Figure countdownlatch and cyclicbarrier based on AQS queue

《7天學會Go並發編程》第7天 go語言並發編程Atomic原子實戰操作含ABA問題

Transformation from student to engineer

管理系统-ITclub(中)

Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)

go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决

管理系统-ITclub(上)

. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method

Ellipsis after SQLite3 statement Solutions for
随机推荐
MONTHS_BETWEEN函数使用
Beijing University of Posts and Telecommunications - multi-agent deep reinforcement learning for cost and delay sensitive virtual network function placement and routing
Secret script of test case design without leakage -- module test
How to do function test well? Are you sure you don't want to know?
Conversation Qiao Xinyu: l'utilisateur est le gestionnaire de produits Wei Brand, zéro anxiété définit le luxe
Example of using gbase 8A OLAP function group by grouping sets
AQS SOS AQS with me
结构化机器学习项目(一)- 机器学习策略
A method of go accessing gbase 8A database
Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
Interview question 3 of software test commonly used by large factories (with answers)
Figure countdownlatch and cyclicbarrier based on AQS queue
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
管理系统-ITclub(中)
This set of steps for performance testing using JMeter includes two salary increases and one promotion
Summary of gbase 8A database user password security related parameters
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
Is flush stock trading software reliable?? Is it safe?
Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury