可以免费做简历的网站,wordpress 自建网站,微信h5怎么制作,成都网站制作服务一、vue 项目搭建
1、创建 vue 项目
vue create vue-element说明#xff1a;创建过程中可以选择路由#xff0c;也可也可以不选择#xff0c;可以通过 npm install 安装
vue 项目目录结构 说明#xff1a;api 为自己创建的文件夹#xff0c;router 选择路由模块会自动…一、vue 项目搭建
1、创建 vue 项目
vue create vue-element说明创建过程中可以选择路由也可也可以不选择可以通过 npm install 安装
vue 项目目录结构 说明api 为自己创建的文件夹router 选择路由模块会自动创建
router下的index.js文件配置路由的文件
import { createRouter, createWebHashHistory } from vue-routerconst routes [{path: /, //路由路径name: login,component: () import(/* webpackChunkName: about */ /views/login.vue)//访问的页面},
]
// 创建路由
const router createRouter({history: createWebHashHistory(),routes
})
// 导出路由
export default routerviews下的 login.vue
templatediv classcontentdiv classloginspan classfont智慧车库管理/spandiv账 号el-inputv-modeluser.accountstylewidth: 240pxplaceholder请输入账号//divdiv stylemargin-top: 20px密 码el-inputv-modeluser.passwordstylewidth: 240pxplaceholder请输入密码typepasswordshow-password//divdiv classregisterdivspan注册账号/span/divdivspan验证码登录/span/div/divdiv clickloginel-button typesuccess round sizelarge classloginbut登录/el-button/div/div/div
/templatescript setup
import { reactive , getCurrentInstance} from vue;const user reactive({ account: , password: });const {proxy} getCurrentInstance()const login async() {console.log(proxy.$urls.m().login);const obj {account:user.account,password:user.password}const res await new proxy.$request(proxy.$urls.m().login,obj).modepost()};
/scriptstyle scoped
.content {height: 100vh;display: flex;justify-content: center;align-items: center;background-color: aliceblue;
}
.login {display: flex;background-color: #ffffff;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);border-radius: 5px;transition: transform 0.3s ease;width: 600px;height: 400px;flex-direction: column;align-items: center;
}
.login:hover {transform: translateY(-10px); /* 鼠标滑过时向上浮动10px */
}
.font {font-size: 24px;font-family: Arial, Helvetica, sans-serif;margin-bottom: 50px;margin-top: 30px;
}
.register {width: 50%;display: flex;justify-content: space-between;margin-top: 30px;
}
.loginbut {width: 200px;margin-top: 40px;
}
/style
main.js 中全局引入路由
import { createApp } from vue
import App from ./App.vue
import router from ./router // 引入路由
import ElementPlus from element-plus // 引入element引入之前需要安装 参考element官方网站 npm 等方式安装即可
import element-plus/dist/index.css // 引入element
import zhCn from element-plus/es/locale/lang/zh-cn // element 国际化const app createApp(App);
app.use(router); // 使用路由
app.use(ElementPlus, {locale: zhCn,}); // 国际化// 请求地址
import urls from ./api/api.js
app.config.globalProperties.$urls urls
// 请求方法
import request from ./api/request
app.config.globalProperties.$request requestapp.mount(#app); //挂载app 在public下的index.htmlApp.vue 文件内容
templaterouter-view/ // 路由页面显示位置
/templatescript/scriptstyle/style
1、请求封装
1.1、axios 安装
cnpm install axios --save1.2 api 封装(面向对象封装)
在 api 文件夹中创建 api.js。文件内容如下
const url http://localhost:8080; // 基础urlconst urls class {static m() {const login ${url}/login; // 模板语法return {login // 导出 login 地址}}
}
export default urls // 暴露 urls在 api 下创建 header.js 。内容如下
import axios from axios; //导入 axios
import {ElMessageBox } from element-plus // 局部引入 element 弹框虽然在main.js中已经全局引入但是这里是js文件并不是 vue 文件所有要导入//创建 axios 全局配置let instance axios.create({responseType: json, // 期望后端返回的数据类型 jsonheaders: { Content-Type: application/json },// 请求头中添加上传给服务器的数据类型 json
});// http 拦截请求发出之前
instance.interceptors.request.use((config) {let token localStorage.getItem(token);//缓存中获取token if (token) {config.headers.Authorization token; // 请求头中携带 token}return config;},(err) {return Promise.reject(err);}
);// http 拦截 请求发出之后
instance.interceptors.response.use((response) {return response;},(error) {if(error.response){let ERRS error.response.statuslet MSG error.response.data.datalet errdata ERRS 401 ? MSG : 服务器打盹啦switch(ERRS) { // 401 时特别处理 没有权限case 401: ElMessageBox.alert(没有权限, 提示, {confirmButtonText: 好的,type: error,}).then(res {}).catch(err {})break;}}return Promise.reject(error.response.data)}
)
export default instance在 api 下创建 request.js 文件文件内容如下
import instance from ./header; // 引入 header.js 文件instance 就是我们在 header.js 中暴露出的 axios 实例const request class { // 创建一个请求类constructor(url, arg) { // 构造器赋值this.url url; // urlthis.arg arg; // 参数}modepost() { // 创建 post 请求return new Promise((resolve, reject) {instance.post(this.url, this.arg)// 成功回调.then( res {resolve(res);})// 失败回调.catch( err {reject(err)})});}
// 创建 get 请求modeget() {return new Promise((reject,resolve) {instance.get((this.url))// 成功回调.then(res {resolve(res)})// 失败回调.catch(err {reject(err)})})}
}export default request // 暴露
这三个文件创建完成之后我们可以回过头去看 main.js
import { createApp } from vue
import App from ./App.vue
import router from ./router // 引入路由
import ElementPlus from element-plus // 引入element引入之前需要安装 参考element官方网站 npm 等方式安装即可
import element-plus/dist/index.css // 引入element
import zhCn from element-plus/es/locale/lang/zh-cn // element 国际化const app createApp(App);
app.use(router); // 使用路由
app.use(ElementPlus, {locale: zhCn,}); // 国际化// 请求地址
import urls from ./api/api.js
app.config.globalProperties.$urls urls
// 请求方法
import request from ./api/request
app.config.globalProperties.$request requestapp.mount(#app); //挂载app 在public下的index.html主要看这里
// 请求地址
import urls from ./api/api.js
app.config.globalProperties.$urls urls //全局配置请求地址
// 请求方法
import request from ./api/request
app.config.globalProperties.$request request // 全局配置请求方法然后再去看 login.vue 中的使用这里我只拿过来 js 部分
script setup
import { reactive , getCurrentInstance} from vue;const user reactive({ account: , password: });const {proxy} getCurrentInstance() //对象展开获取 proxyconst login async() {const obj {account:user.account,password:user.password} // 封装请求参数const res await new proxy.$request(proxy.$urls.m().login,obj).modepost() // 创建 request 实例并调用 modepost() 方法发送请求
};
/script到这里vue 项目的基本搭建已经完成
二、spring boot 项目搭建
使用 idea 创建 springboot项目导入web 依赖即可 项目结构 这里比较简单不过都赘述 看一下测试接口LoginController
Slf4j // 日志注解
RestController // 表示是一个controller并且会返回json 数据相当于 Controller 和 ResponseBody 组合
RequestMapping(/login) // 请求地址 /login 支持所有方式的请求
CrossOrigin(origins http://localhost:8082) // 跨域解决允许本机8082程序访问后续会统一处理
public class LoginController {PostMapping // 接受 /login 下的 post 请求public String Login(RequestBody LoginDTO loginDTO){ // RequestBody 接受请求体中的 json 数据并解析为 LoginDTO 对象log.info(loginDTO.toString()); // 日志打印解析后的对象 return ;}
}
LoginDTO 在controller 下的 dto 下内容如下
Data
AllArgsConstructor
NoArgsConstructor // 这三个都是 Lombok 注解简化开发 自动生成gettersetter 构造器等
public class LoginDTO {private String account;private String password;
} 文章转载自: http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.smfbw.cn.gov.cn.smfbw.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.lsnbx.cn.gov.cn.lsnbx.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.zwckz.cn.gov.cn.zwckz.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.prlgn.cn.gov.cn.prlgn.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.cpnsh.cn.gov.cn.cpnsh.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.fksxs.cn.gov.cn.fksxs.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn