开始开发前端部分
讲师列表
先登录改到本地,后期再加一个spring secutiry框架
运行:
npm run build
Shift+Alt+f整理格式
新版的登录url在这里:
src/api/user.js
用的是
http://localhost:9528/dev-api/vue-admin-template/user/login
不用修改BASE_API,因为新版本本身就是用的localhost
创建EduLoginController
1.login
2.info
package com.lkw.eduservice.controller;
import com.lkw.commonutils.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/eduservice/user")
public class EduLoginController {
//login
@PostMapping("login")
public R login(){
return R.ok().data("token","admin");
}
//info
@GetMapping("info")
public R info(){
return R.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
}
}
在src/api/user.js修改地址:
为:/eduservice/user/login

在.env.development可以看到:
‘/dev-api’
修改为:‘’
对,就是空
然后,
在vue.config.js修改成8001

//可以先不改,后面发现改了没用
记得保存
启动idea与vscode
vscode关ctrl+c
重新启动:
npm run dev
改完之后是8002,因为8001被idea占了,到时候转到idea应该就没事
所以把这里改成全地址就能用了
http://localhost:8002/eduservice/user/login

会出现network error
需要端口号相同
方法一:controller添加注解:
就能够跨域访问
每个controller都要加
方法二:使用网关
寻找路由功能在前端的哪个地方
在src/router/index.js
这里会路由到views创建的vue页面
在vue页面会调用api文件夹的js文件,然后实现功能
/src/router/index
{
path: '/teacher',
component: Layout,
redirect: '/taecher/table',
name: '讲师管理',
meta: { title: '讲师管理', icon: 'el-icon-s-help' },
children: [
{
path: 'table',
name: '讲师列表',
component: () => import('@/views/edu/teacher/list'),
meta: { title: '讲师列表', icon: 'table' }
},
{
path: 'tree',
name: '添加讲师',
component: () => import('@/views/edu/teacher/save'),
meta: { title: '添加讲师', icon: 'tree' }
}
]
},
创建文件:
src\views\edu\teacher\list.vue
src\views\edu\teacher\save.vue
显示的信息
创建:src\api\teacher\teacher.js
import request from '@/utils/request'
export function getTeacherListPage(current,limit,teacherQuery) {
return request({
//细节:url后面是`不是'
url: ` http://localhost:8001/eduservice/teacher/pageTeacherCondition/${current}/${limit}`,
method: 'post',
//teacherQuery条件对象,后端使用RequestBody获取数据
//data表示把对象转换json进行传递到接口里面
data:teacherQuery
})
}
在,src\views\edu\teacher\list.vue调用写好的js方法
main.vue封装好了 new Vue
主要是data,created,method
讲师列表
显示数据:
使用element-ui实现
地址:https://element.eleme.cn/#/zh-CN/component/layout
两个等号判断值
三个等号判断值,也判断类型
list.vue代码:
讲师列表
{{ (page - 1) * limit + scope.$index + 1 }}
{{ scope.row.level===1?'高级讲师':'首席讲师' }}
修改
删除

在element-ui找一个分页条
加在
添加默认页码:
list.vue完整代码:
讲师列表
{{ (page - 1) * limit + scope.$index + 1 }}
{{ scope.row.level===1?'高级讲师':'首席讲师' }}
修改
删除
组件代码
查 询
清空
清空按钮已存在
清空方法:
resetData(){// 清空的方法
//表单输入项数据清空
this.teacherQuery = {}
//查询所有讲师数据
this.getList
}
记得打逗号
在teacher.js补充方法:
//删除讲师
deleteTeacherId(id){
return request({
url: `http://localhost:8001/eduservice/teacher/${id}`,
method: 'delete'
})
}
在这里用的全地址,因为不写全就访问到localhost:9528了
记得打逗号
在list.vue添加方法:
removeDataById(id) {
this.$confirm("此操作将永久删除讲师记录, 是否继续?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}).then(() => {
// 点击确定,执行then方法
teacher.deleteTeacherId(id)
.then((response) => {
//删除成功
//提示消息
this.$message({
type: "success",
message: "删除成功!",
});
//回到列表页面
this.getList(this.page);
});
});
},
teacher.js添加代码:
注意逗号
import request from '@/utils/request'
export default{
getTeacherListPage(current,limit,teacherQuery) {
return request({
//细节:url后面是`不是'
url: ` http://localhost:8001/eduservice/teacher/pageTeacherCondition/${current}/${limit}`,
method: 'post',
//teacherQuery条件对象,后端使用RequestBody获取数据
//data表示把对象转换json进行传递到接口里面
data: teacherQuery
})
},
//删除讲师
deleteTeacherId(id){
return request({
url: ` http://localhost:8001/eduservice/teacher/${id}`,
method: 'delete'
})
},
//添加讲师
addTeacher(teacher){
return request({
url: ` http://localhost:8001/eduservice/teacher/addTeacher`,
method: 'post',
data: teacher
})
}
}
在save.vue添加代码:
讲师添加
保存
在src/rou

,
{
path: 'save/:id',
name: 'EduTeacherEdit',
component: () => import('@/views/edu/teacher/save'),
meta: { title: '编辑讲师', noCache: true },
hidden: true
}
save/:id的id相当于占位符
修改list.vue的修改按钮的路径
然后,点击修改就会重定向到save路径
数据回显:
定义一个接口在teacher.js
,
getTeacherInfo(id){
return request({
url: `http://localhost:8001/eduservice/teacher/getTeacher/${id}`,
method: 'get'
})
},
在save.vue
//根据讲师id查询方法
getInfo(id){
teacherApi.getTeacherInfo(id)
.then(response=>{
this.teacher=response.data.teacher
})
},
注意逗号
调用id查询的方法,如果id为null就不调用,有id就查询回显
使用created检查参数
save.vue的created:
if (this.$route.params && this.$route.params.id) {
//从路径获取id值
const id = this.$route.params.id;
//调用根据id查询的方法
this.getInfo(id);
}
teacher.js里添加修改讲师方法:
,
//修改讲师
updateTeacherInfo(teacher) {
return request({
url: `/eduservice/teacher/updateTeacher`,
method: 'post',
data: teacher
})
}
save.vue添加修改讲师的方法
,
//修改讲师的方法
updateTeacher(){
teacherApi.updateTeacherInfo(this.teacher)
.then(response => {
//提示信息
this.$message({
type: "success",
message: "修改成功!",
});
//回到列表页面 路由跳转
this.$router.push({ path: "/teacher/table" });
})
},
判断修改还是添加:
更改saveOrUpdate方法的内容:
saveOrUpdate() {
//判断是修改还是添加
if (!this.teacher.id) {
//添加
this.saveTeacher();
} else {
//修改
this.updateTeacher();
}
},
点了修改讲师会出现数据回显,再点添加讲师,数据不会消失,所以添加讲师的时候表单数据清空
可以用vue监听
可以监听id然后调用清空方法
//但是用的新版本的前端模板没有这个情况,该问题跳过