前后端联调总结
程序员文章站
2022-06-03 21:35:22
...
一、前后端联调的具体过程
1、swagger测试
2、测试api:浏览器network页签的xhr中的请求响应是否成功
3、测试数据绑定
4、数据渲染:模板中的html
二、具体功能实现
1、teacher.js,api模块,用来定义接口调用后端实现的接口
// @ 符号在build/webpack.base.conf.js 中配置 表示 'src' 路径
import request from '@/utils/request'
export default{
// 查询教师列表
list() {
return request({
url: '/admin/edu/teacher/list',
method: 'get'
})
},
// 分页查询讲师
pageList(page, limit, searchObj) {
return request({
url: `/admin/edu/teacher/list/${page}/${limit}`,
method: 'get',
params: searchObj
})
},
// 根据id删除讲师
removeById(id) {
return request({
url: `/admin/edu/teacher/remove/${id}`,
method: 'delete'
})
},
// 新增讲师
save(teacher) {
return request({
url: '/admin/edu/teacher/save',
method: 'post',
data: teacher
})
},
// 通过id获取讲师信息
getById(id) {
return request({
url: `/admin/edu/teacher/get/${id}`,
method: 'get'
})
},
// 通过id修改讲师信息
updateById(teacher) {
return request({
url: `/admin/edu/teacher/update`,
method: 'put',
data: teacher
})
}
}
2、list.vue,查询,分页,删除,修改,页面通过element-ui渲染
<!--
vue页面由2至3部分组成:<template>、<script>、<style>
-->
<template>
<div>
<el-form :inline="true">
<el-form-item>
<el-input v-model="searchObj.name" placeholder="讲师"/>
</el-form-item>
<el-form-item>
<el-select v-model="searchObj.level" placeholder="头衔">
<el-option label="高级讲师" value="1"/>
<el-option label="首席讲师" value="2"/>
</el-select>
</el-form-item>
<el-date-picker
v-model="searchObj.joinDateBegin"
value-format="yyyy-MM-dd"
align="right"
type="date"
placeholder="入驻时间"/>
<el-form-item>
<el-date-picker
v-model="searchObj.joinDateEnd"
value-format="yyyy-MM-dd"
align="right"
type="date"
placeholder="结束时间"/>
<el-form-item>
<el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
<el-button type="default" @click="resetData()">清空</el-button>
</el-form-item>
</el-form-item></el-form>
<!-- 表格 -->
<el-table :data="list" border stripe>
<el-table-column width="50" label="#">
<template slot-scope="scope">
{{ (page-1)*limit+scope.$index+1 }}
</template>
</el-table-column>
<el-table-column prop="name" label="名称" width="80" />
<el-table-column label="头衔" width="90">
<template slot-scope="scope">
<el-tag v-if="scope.row.level === 1" type="success" size="mini">高级讲师</el-tag>
<el-tag v-if="scope.row.level === 2" size="mini">首席讲师</el-tag>
</template>
</el-table-column>
<el-table-column prop="intro" label="简介" />
<el-table-column prop="sort" label="排序" width="60" />
<el-table-column prop="joinDate" label="入驻时间" width="160" />
<el-table-column label="操作" width="200" align="center">
<template slot-scope="scope">
<el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.id)">删除</el-button>
<router-link :to="'/teacher/edit/'+scope.row.id">
<el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
</router-link>
</template>
</el-table-column>
</el-table>
<!-- 分页组件 -->
<el-pagination
:current-page="page"
:page-sizes="[5, 10, 20]"
:page-size="limit"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
style="padding: 30px 0; text-align: center;"
@size-change="changePageSize"
@current-change="changeCurrentPage"/>
</div>
</template>
<script>
import teacherApi from '@/api/teacher.js'
export default {
// 定义数据类型
data() {
return {
list: [], // 讲师列表
toatl: 0, // 总记录数
page: 1, // 页码
limit: 5, // 每页记录数
searchObj: {}// 查询条件
}
},
// 页面渲染成功后获取数据
created() {
this.fetchData()
},
// 定义方法
methods: {
// 删除讲师
removeById(id) {
this.$confirm('此操作将永久删除该讲师, 是否继续?', '提示', {
confirmButtonText: '中',
cancelButtonText: '不中',
type: 'warning'
}).then(() => {
return teacherApi.removeById(id)
}).then(response => {
this.fetchData()
this.$message.success(response.message)
}).catch(error => {
// 当取消时会进入catch语句:error = 'cancel'
// 当后端服务抛出异常时:error = 'error'
if (error === 'cancel') {
this.$message.info('取消删除')
}
})
},
// 清空
resetData() {
this.searchObj = {}
this.fetchData()
},
// 改变每页记录数,size:回调参数,表示当前选中的“每页条数”
changePageSize(size) {
this.limit = size
this.fetchData()
},
// 改变当前页数,page:回调参数,表示当前选中的“页码”
changeCurrentPage(page) {
this.page = page
this.fetchData()
},
fetchData() {
// 调用api
// teacherApi.list().then(response => {
// this.list = response.data.items
// })
teacherApi.pageList(this.page, this.limit, this.searchObj).then(response => {
// this.list = response.data.items
this.list = response.data.pageModel.records
this.total = response.data.pageModel.total
})
}
}
}
</script>
3、form.vue,用来做页面渲染,导入api模块
<template>
<div class="app-container">
<!-- 输入表单 -->
<el-form label-width="120px">
<el-form-item label="讲师名称">
<el-input v-model="teacher.name" />
</el-form-item>
<el-form-item label="入驻时间">
<el-date-picker v-model="teacher.joinDate" value-format="yyyy-MM-dd" />
</el-form-item>
<el-form-item label="讲师排序">
<el-input-number v-model="teacher.sort" :min="0"/>
</el-form-item>
<el-form-item label="讲师头衔">
<el-select v-model="teacher.level">
<!--
数据类型一定要和取出的json中的一致,否则没法回填
因此,这里value使用动态绑定的值,保证其数据类型是number
-->
<!-- value前必须加:,否则是字符串 -->
<el-option :value="1" label="高级讲师"/>
<el-option :value="2" label="首席讲师"/>
</el-select>
</el-form-item>
<el-form-item label="讲师简介">
<el-input v-model="teacher.intro"/>
</el-form-item>
<el-form-item label="讲师资历">
<el-input v-model="teacher.career" :rows="10" type="textarea"/>
</el-form-item>
<!-- 讲师头像:TODO -->
<el-form-item>
<el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">保存</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
import teacherApi from '@/api/teacher.js'
export default {
// 定义数据类型
data() {
return {
// 初始化讲师默认数据
teacher: {
sort: 0,
level: 1
},
saveBtnDisabled: false// 保存按钮是否禁用,防止表单重复提交
}
},
// 页面渲染成功后获取数据
created() {
// 当回显时调用,获取当前记录的id
if (this.$route.params.id) {
this.fetchDataById(this.$route.params.id)
}
},
// 定义方法
methods: {
// 根据id查询讲师
fetchDataById(id) {
teacherApi.getById(id).then(response => {
this.teacher = response.data.teacher
})
},
saveOrUpdate() {
// 保存按钮开启禁用
this.saveBtnDisabled = true
// 判断新增还是修改
if (!this.teacher.id) {
this.saveData()
} else {
this.updateData()
}
},
// 修改讲师
updateData() {
teacherApi.updateById(this.teacher).then(response => {
this.$message.success(response.message)
// 跳转到讲师列表,通过路由跳转
this.$router.push({ path: '/teacher' })
})
},
// 保存讲师
saveData() {
teacherApi.save(this.teacher).then(response => {
this.$message.success(response.message)
// 跳转到讲师列表,通过路由跳转
this.$router.push({ path: '/teacher' })
})
}
}
}
</script>