springboot+vue—21


添加一个路由(隐藏路径,不显示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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我柒月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值