vue 中子$emit 根据父组件方法的返回值 做处理

需求:点击新增弹框增加一条数据的时候,点击弹框确定的时候判断已添加的数据中是否有相同的数据,如有相同数据提示“已添加”

实现方式1
通过props 传参数,把已经添加过的数据(tableData)传到谈框里,弹框点击确定的时候,判断新添加的数据是否存在tableData 中

props:{
    //表格已添加数据
    tableData: {
      type: Array,
      default: () => ([]),
    },
  },
// 子组件保存
saveData(){
      this.loading = true
      this.confirmLoading = true
      const data = Object.assign({}, this.form)
      const creditCodes = this.tableData.map(item => item.creditCode)
      if (creditCodes.includes(data.creditCode)) {
          this.$message.warning('已添加!')
          this.$refs['form'].resetFields()
	      this.visible = false
	      this.confirmLoading = false
	      this.loading = false
          return
      } else {
          this.confirmLoading = false
          this.loading = false
        }
      

    },

实现方式2:
通常子组件调用父组件方法:this.$emit(方法名, 传参1, 传参2),但是此方法的返回值是vue对象,而不是父组件方法的return值。此时要用到高阶函数,传函数作为参数,父组件里执行该函数。

//父组件新增--确定
    handleAddOk(pData, callback){
      let flag = true
      const creditCodes = this.tableData.map(item => item.creditCode)
      if (creditCodes.includes(pData.creditCode)) {
        this.$message.warning('已添加!')
        flag = false
        callback(flag)
        return
      }
      callback(flag)
      this.tableData.unshift(pData)
      this.$refs.table.setTableData(this.tableData)
      // this.$nextTick(() => this.$refs.table.refresh())
    },
// 子组件
saveData(){
      this.loading = true
      this.confirmLoading = true
      const data = Object.assign({}, this.form)
      this.$emit('add', data, flag =>{
        if (flag) {
          this.$refs['form'].resetFields()
          this.visible = false
          this.confirmLoading = false
          this.loading = false
        } else {
          this.confirmLoading = false
          this.loading = false
        }
      })
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值