使用Vue+MySQL實(shí)現(xiàn)登錄注冊(cè)的實(shí)戰(zhàn)案例
1.新建vue項(xiàng)目并連接數(shù)據(jù)庫(kù)
具體步驟見(jiàn)vue連接mysql數(shù)據(jù)庫(kù)
2.新建登錄頁(yè)面、注冊(cè)頁(yè)面和首頁(yè)
在src/views文件夾下,新建 login.vue(登錄頁(yè)面)、register.vue(注冊(cè)頁(yè)面) 和 home.vue(首頁(yè))
根據(jù)自己的喜好搭建頁(yè)面(本人此處使用了elementUI的組件,cv前要先安裝elementUI中間件)
npm i element-ui -S
<template>
<div class="bg">
<div id="login">
<h2>登錄頁(yè)面</h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label="用戶(hù)名:">
<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>
<a @click="register" class="reg">前往注冊(cè)</a>
</div>
</div>
</template>
<template>
<div class="bg">
<div id="register">
<h2>注冊(cè)頁(yè)面</h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label="用戶(hù)名:">
<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">注冊(cè)</el-button>
</div>
</div>
</template>
<template>
<div id="main">
<el-container>
<el-header>
<div class="logo" >
<img src="../assets/img/logo.png"> <!-- 此處請(qǐng)?zhí)崆皽?zhǔn)備好圖片 -->
</div>
<div class="user">
{{username}}
</div>
</el-header>
<el-main>main</el-main>
<el-footer>Footer</el-footer>
</el-container>
</div>
</template>
頁(yè)面效果



3.頁(yè)面路由配置
在src/router/index.js中配置頁(yè)面對(duì)應(yīng)路由
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
})
//解決相同路徑跳轉(zhuǎn)報(bào)錯(cuò)問(wèn)題
//使用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 傳遞來(lái)的參數(shù),通過(guò)查詢(xún)語(yǔ)句查詢(xún)對(duì)應(yīng)數(shù)據(jù)并放回結(jié)果
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: '用戶(hù)名或密碼錯(cuò)誤'
})
}
})
}
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: '用戶(hù)名已存在'
})
}else{
db.query(sql2, [req.body.params.name, req.body.params.password], (err, data) => {
if(err) {
return res.send({
status: 400,
message: "注冊(cè)失敗"
})
}
res.send({
status: 200,
message: "注冊(cè)成功"
})
})
}
})
}
5.在/server/router.js中配置對(duì)應(yīng)路由
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中編寫(xiě)相應(yīng)方法
<template>
<div class="bg">
<div id="login">
<h2>登錄頁(yè)面</h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label="用戶(hù)名:">
<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('用戶(hù)名不能為空');
}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('用戶(hù)名或密碼錯(cuò)誤', '登錄失敗', {
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>注冊(cè)頁(yè)面</h2>
<el-form ref="form" :model="form" label-width="20%">
<el-form-item label="用戶(hù)名:">
<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">注冊(cè)</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('用戶(hù)名不能為空');
}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('是否返回登錄頁(yè)面', '注冊(cè)成功', {
confirmButtonText: '確定',
callback: action => {
this.$router.push('/login')
}
})
}else if(res.data.status == 202) {
this.$alert('用戶(hù)名已存在', '注冊(cè)失敗', {
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"> <!-- 此處請(qǐng)?zhí)崆皽?zhǔn)備好圖片 -->
</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() { //頁(yè)面創(chuàng)建時(shí),把路由傳遞來(lái)的用戶(hù)名賦值給data中的username,這樣就可以在頁(yè)面顯示用戶(hù)名了(效果見(jiàn)首頁(yè)的右上角)
this.username = this.$route.query.name;
}
}
</script>
效果展示

登錄注冊(cè)demo
git源碼地址:https://gitee.com/xie-xiaochun/login-registration-demo
注意:資源中不包含數(shù)據(jù)庫(kù),需自己創(chuàng)建數(shù)據(jù)庫(kù),并修改源碼中數(shù)據(jù)庫(kù)的相關(guān)信息。
總結(jié)
到此這篇關(guān)于使用Vue+MySQL實(shí)現(xiàn)登錄注冊(cè)的文章就介紹到這了,更多相關(guān)Vue+MySQL登錄注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue整合Node.js直連Mysql數(shù)據(jù)庫(kù)進(jìn)行CURD操作過(guò)程詳解
- Node.js+Express+Vue+MySQL+axios的項(xiàng)目搭建全過(guò)程
- Vue項(xiàng)目通過(guò)node連接MySQL數(shù)據(jù)庫(kù)并實(shí)現(xiàn)增刪改查操作的過(guò)程詳解
- Vue+ElementUI?實(shí)現(xiàn)分頁(yè)功能-mysql數(shù)據(jù)
- Vue實(shí)現(xiàn)模糊查詢(xún)-Mysql數(shù)據(jù)庫(kù)數(shù)據(jù)
- 如何在Vue3項(xiàng)目中操作MySQL數(shù)據(jù)庫(kù)
相關(guān)文章
vue使用lodop打印控件實(shí)現(xiàn)瀏覽器兼容打印的方法
這篇文章主要介紹了vue使用lodop打印控件實(shí)現(xiàn)瀏覽器兼容打印的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
解決webpack-bundle-analyzer的問(wèn)題大坑
這篇文章主要介紹了解決webpack-bundle-analyzer的問(wèn)題大坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
解決ant Design中this.props.form.validateFields未執(zhí)行的問(wèn)題
這篇文章主要介紹了解決ant Design中this.props.form.validateFields未執(zhí)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Vue中.vue文件比main.js先執(zhí)行的問(wèn)題及解決
這篇文章主要介紹了Vue中.vue文件比main.js先執(zhí)行的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vuex通過(guò)getters訪(fǎng)問(wèn)數(shù)據(jù)為undefined問(wèn)題及解決
這篇文章主要介紹了vuex通過(guò)getters訪(fǎng)問(wèn)數(shù)據(jù)為undefined問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue動(dòng)態(tài)代理無(wú)須重啟項(xiàng)目解決方案詳解
這篇文章主要為大家介紹了vue動(dòng)態(tài)代理無(wú)須重啟項(xiàng)目解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

