新增和编辑使用同一个页面

<template>
  <div class="container">
    <el-button
      type="primary"
      icon="el-icon-plus"
      class="table-nav-btn"
      @click="goToDetail('')"
    >新增</el-button>

    <el-table-column label="操作" align="left" fixed="right" width="120px">
      <!-- 使用作用域插槽拿到当前行id 注:新增不需要id 编辑需要id -->
      <template slot-scope="scope">
        <div class="jh-space-between">
          <el-link
            type="primary"
            @click="goToDetail(scope.row.id)"
          >编辑</el-link>
        </div>
      </template>
    </el-table-column>
  </div>
</template>

<script>
export default {
  methods: {
    // 新增/编辑-跳转
    goToDetail(id) {
      this.$router.push({
        path: '/marketingManagement/setMealActivity/index/details',
        query: { 
          id,
          isAdd: !id
        }
      })
    }
  }
}
</script>

新增&编辑页面

  mounted() {
    const { id } = this.$route.query //获取id
    if (id) {
      // 根据有无id判断 有id则是编辑 调用接口获取数据回显
      this.getSetMealDetail(id)
    }
    this.type = id ? 'edit' : 'add' //判断 无id则是新增 type类型为新增
    this.form.id = id || ''
    if (this.type === 'add') {
     //type类型为新增 处理新增的逻辑
      this.form.coverUrl = this.setMealCoverUrl
      this.$nextTick(() => {
        this.courseCover = [{ url: this.setMealCoverUrl }]
      })
    }
  },
  methods: {
   //调取编辑页面回显的数据
    getSetMealDetail(id) {
      getSetMealDetail({ id }).then((res) => {
        //这里通过接口获取数据 赋值
      })
    },
    // 新增&编辑表单后提交
    submit() {
      this.$refs.form.validate((valid) => {
        // 表单校验
        if (valid) {
          const data = Object.assign({}, this.form)
          if (this.type === 'edit') { // 判断如果是编辑则调取编辑接口
            this.editSetMeal(data)
          } else {
            this.addSetMeal(data)
          }
        } else {
          return false
        }
      })
    }
  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值