添加一个路由(隐藏路径,不显示icon那种)
hidden:true
要与如下路由对应上
<router-link :to="'/teacher/edit/'+scope.row.id">
{
path: 'edit/:id',
name: '修改讲师',
component: () => import('@/views/edu/teacher/save'),
meta: { title: '修改讲师', noCache:true },
hidden:true
}
1.teacher.js编写函数
//根据id查询讲师
getTeacherInfo(id){
return request({
url:`/eduservice/teacher/getTeacher/${id}`,
method:'get',
})
}
2.前端js部分
//根据id查询讲师
getInfo(id){
teacherApi.getTeacherInfo(id)
.then(response=>{
this.teacher=response.data.teacher
})
},
如何调用这个函数?(添加的时候不需要调用,修改的时候调用)
如果路径中有id值,就调用,没有就不调用(调用添加函数)
created(){
this.init()
},
init(){
if (this.$route.params && this.$route.params.id) {
const id = this.$route.params.id
this.getInfo(id)
}else{
this.teacher={}
}
},
保存的时候判断是修改的还是添加的保存
<el-form-item>
<el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
</el-form-item>
saveOrUpdate(teacher){
//判断是修改还是添加
if(this.teacher.id){
this.updateTeacher()
}else{
this.saveTeacher()
}
},
3.完整代码
<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-input-number v-model="teacher.sort" controls-position="right" />
</el-form-item>
<el-form-item label="讲师头衔">
<el-select v-model="teacher.level" clearable placeholder="请选择">
<!--
数据类型一定要和取出的json中的一致,否则没法回填
因此,这里value使用动态绑定的值,保证其数据类型是number
-->
<el-option :value="1" label="高级讲师"/>
<el-option :value="2" label="首席讲师"/>
</el-select>
</el-form-item>
<el-form-item label="讲师资历">
<el-input v-model="teacher.career"/>
</el-form-item>
<el-form-item label="讲师简介">
<el-input v-model="teacher.intro" :rows="10" type="textarea"/>
</el-form-item>
<!-- 讲师头像:TODO -->
<!-- 讲师头像 -->
<el-form-item label="讲师头像">
<!-- 头衔缩略图 -->
<pan-thumb :image="teacher.avatar"/>
<!-- 文件上传按钮 -->
<el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
</el-button>
<!--
v-show:是否显示上传组件
:key:类似于id,如果一个页面多个图片上传控件,可以做区分
:url:后台上传的url地址
@close:关闭上传组件
@crop-upload-success:上传成功后的回调 -->
<image-cropper
v-show="imagecropperShow"
:width="300"
:height="300"
:key="imagecropperKey"
:url="BASE_API+'eduoss/fileoss'"
field="file"
@close="close"
@crop-upload-success="cropSuccess"/>
</el-form-item>
<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/edu/teacher'
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
export default {
components: { ImageCropper, PanThumb, },
data(){
return{
teacher:{
name: '',
sort: 0,
level: 1,
career: '',
intro: '',
avatar:''
},
// 是否显示上传组件
imagecropperShow:false,
// 上传组件id
imagecropperKey:0,
// 接口API地址
BASE_API: process.env.BASE_API,
saveBtnDisabled: false // 保存按钮是否禁用,
}
},
created(){
this.init(),
//https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3201746467,2179502918&fm=26&gp=0.jpg
this.teacher.avatar='https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3201746467,2179502918&fm=26&gp=0.jpg'
},
//监听
watch:{
$route(to,from){
this.init()
}
},
methods:{
//关闭上传弹框
close(){
this.imagecropperShow=false
},
//上传成功
cropSuccess(data){
//上传成功返回图片地址
this.imagecropperShow=false,
//初始化上传文件框
this.imagecropperKey=imagecropperKey+1,
this.teacher.avatar=data.url
},
init(){
if (this.$route.params && this.$route.params.id) {
const id = this.$route.params.id
this.getInfo(id)
}else{
this.teacher={}
}
},
//根据id查询讲师
getInfo(id){
teacherApi.getTeacherInfo(id)
.then(response=>{
this.teacher=response.data.teacher
})
},
saveOrUpdate(teacher){
//判断是修改还是添加
if(this.teacher.id){
this.updateTeacher()
}else{
this.saveTeacher()
}
},
//修改讲师
updateTeacher(){
teacherApi.updateById(this.teacher)
.then(response=>{
this.$message({
type: 'success',
message: '修改成功!'
})
this.$router.push({ path: '/teacher/table' })
})
},
//添加讲师
saveTeacher(teacher){
teacherApi.addTeacher(this.teacher)
.then(response=>{
this.$message({
type: 'success',
message: '添加成功!'
})
this.$router.push({ path: '/teacher/table' })
})
}
}
}
</script>