php网站开发淮安招聘,芒市网站建设公司,网站有信心做的更好,公众号官方前提#xff1a;后端jar环境、Vue3环境、Redis环境
搭建页面#xff08;html标签、css样式#xff09; → 绑定数据与事件#xff08;表单校验#xff09; → 调用后台接口#xff08;接口文档、src/api/xx.js封装、页面函数中调用#xff09; Login.vue文件#xff…前提后端jar环境、Vue3环境、Redis环境
搭建页面html标签、css样式 → 绑定数据与事件表单校验 → 调用后台接口接口文档、src/api/xx.js封装、页面函数中调用 Login.vue文件
script setup
import { User, Lock } from element-plus/icons-vue;
import { ref } from vue;
//控制注册与登录表单的显示 默认false显示登录 true时显示注册
const isRegister ref(false);//定义数据模型
const registerData ref({username: ,password: ,rePassword: ,
});
//校验密码的函数
const checkRePassword (rule, value, callback) {if (value ) {callback(new Error(请再次确认密码));} else if (value ! registerData.value.password) {callback(new Error(请确保两次输入的密码一样));} else {callback();}
};
//定义表单校验规则
const rules {username: [{ required: true, message: 请输入用户名, trigger: blur },{ min: 5, max: 16, message: 长度为5~16位非空字符, trigger: blur },],password: [{ required: true, message: 请输入密码, trigger: blur },{ min: 5, max: 16, message: 长度为5~16位非空字符, trigger: blur },],rePassword: [{ validator: checkRePassword, trigger: blur }],
};// 调用后台接口完成注册
import {userRegisterService} from /api/user.js
const registerasync(){// registerData是一个响应式对象如果要获取值需要.valuelet resultawait userRegisterService(registerData.value);if(result.code0){alert(result.msg?result.msg:注册成功);}else{alert(注册失败)}
}
/scripttemplate
!-- 登录和注册是同一个页面 --el-row classlogin-pageel-col :span12 classbg/el-colel-col :span6 :offset3 classform
!-- 注册表单 --el-form refform sizelarge autocompleteoff v-ifisRegister :modelregisterData :rulesrulesel-form-itemh1注册/h1/el-form-item!-- 用户名 --el-form-item propusernameel-input :prefix-iconUser placeholder请输入用户名 v-modelregisterData.username/el-input/el-form-item!-- 密码 --el-form-item proppasswordel-input :prefix-iconLock typepassword placeholder请输入密码 v-modelregisterData.password /el-input/el-form-item!-- 确认密码 --el-form-item proprePasswordel-input :prefix-iconLock typepassword placeholder请输入再次密码 v-modelregisterData.rePassword /el-input/el-form-item
!-- 注册按钮 --el-form-itemel-button classbutton typeprimary auto-insert-space clickregister注册/el-button/el-form-itemel-form-item classflexel-link typeinfo :underlinefalse clickisRegister false← 返回/el-link/el-form-item/el-form!-- 登录表单 --!-- 登录按钮 --/el-col/el-row
/templatestyle langscss scoped
/* 样式 */
.login-page {height: 100vh;background-color: #fff;.bg {background: url(/assets/logo2.png) no-repeat 60% center / 240px auto,url(/assets/login_bg.jpg) no-repeat center / cover;border-radius: 0 20px 20px 0;}.form {display: flex;flex-direction: column;justify-content: center;user-select: none;.title {margin: 0 auto;}.button {width: 100%;}.flex {width: 100%;display: flex;justify-content: space-between;}}
}
/style
api文件夹下的user.js
// 导入request.js请求工具
import request from /utils/request.js
//提供调用注册接口的函数
export const userRegisterService(registerData){// 借助于URLsearchParams完成传递const paramsnew URLSearchParams()for(let key in registerData){params.append(key,registerData[key]);}return request.post(/user/register,params);
}
解决跨域问题解决浏览器的同源策略限制向不同源发送Ajax请求会失败 使用配置代理
①utils文件夹下的request.js文件
//定制请求的实例//导入axios npm install axios
import axios from axios;
//定义一个变量,记录公共的前缀 , baseURL
const baseURL /api;
const instance axios.create({baseURL})//添加响应拦截器
instance.interceptors.response.use(result{return result.data;},err{alert(服务异常);return Promise.reject(err);//异步的状态转化成失败的状态}
)export default instance;//向外暴露
②修改vite.config.js
import { fileURLToPath, URL } from node:urlimport { defineConfig } from vite
import vue from vitejs/plugin-vue
import path from node:path// https://vitejs.dev/config/
export default defineConfig({plugins: [vue(),],resolve: {alias: {: fileURLToPath(new URL(./src, import.meta.url))}},server:{proxy:{/api:{//获取路径中包含/api的请求target:http://localhost:9999,//后台服务所在的源changeOrigin:true,//修改源rewrite:(path)path.replace(/^\/api/,)//将/api替换为空字符串}}}
}) 文章转载自: http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.qdlnw.cn.gov.cn.qdlnw.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.zlchy.cn.gov.cn.zlchy.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.zfcfk.cn.gov.cn.zfcfk.cn http://www.morning.wpsfc.cn.gov.cn.wpsfc.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn