Vue.js开发中面向对象开发总结

文章讲述了在Vue.js开发中如何通过组件继承实现添加和编辑功能的共用布局,但这种方式可能导致编辑组件无法添加额外控件。为解决这个问题,文中提出了使用Vue的插槽功能,允许子组件自定义部分区域,从而在保持父组件结构的同时,实现差异化布局。通过插槽,组件B可以对组件A的部分进行覆写,实现了更灵活的页面构建和数据传递。
摘要由CSDN通过智能技术生成

在Vue.js开发中,组件编写时添加和编辑功能页面布局一直,区别在于编辑时需要数据还原填充。

在面向对象思想里只需要编辑组件继承添加组件即可,那么Vue中怎么操作呢。

添加页面

<template>
  <el-dialog
    :title="titleType"
    :visible.sync="dialog"
    center
    :close-on-click-modal="false"
    :before-close="handleClose"
    :destroy-on-close="true"
    @close="vueclosed"
    @open="openvue"
    width="60%"
  >
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
           <el-form-item label="职位标题" prop="jobTitle">
            <el-input v-model="ruleForm.jobTitle"
                      placeholder="请填写客户名称"
                      clearable
            >
            </el-input>
          </el-form-item>
    </el-form>
</el-dialog>
</template>
export default {
  name: 'LLAddJob'
}

编辑页面

<script>
import LLAddJob from './LLAddJob'
export default {
  name: 'LLEditJob',
  extends: LLAddJob
}
</script>

用 extends 继承即可

但是这样有一个缺陷,就是编辑组件不能在页面上添加任何控件,否则会对父组件进行重写覆盖,

如果子组件的压面控件多余或者有不同于父组件的地方,怎么办呢?只能在父组件中添加数据字段,在页面显示的时候加以判断,进行不同的布局显示。这样做显然是不合理的,那要怎么做呢?

对,用到了Vue.的插槽功能。具体如下:

组件A

<template>
  <el-dialog
    :title="titleType"
    :visible.sync="dialog"
    center
    :close-on-click-modal="false"
    :before-close="handleClose"
    :destroy-on-close="true"
    @close="vueclosed"
    @open="openvue"
    width="40%"
  >
    <el-row :gutter="0" >
      <el-col :span="24">
        <el-descriptions  :column="2">
          <el-descriptions-item  label="客户名称" label-class-name='my-label'> 
             {{ruleForm.customerName}}
          </el-descriptions-item>
      </el-col>
      <el-col>
        <slot name="footer">
          <div style="float:right;">
            <!--        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>-->
            <el-button @click="handleClose">取消</el-button>
          </div>
        </slot>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  name: 'CustomerDetail',
  props: {
    dialog: Boolean,
    acountData: {}
  },
  data () {
  }
}

组件B

<template>
   <CustomerDetail ref="customerDetail"
                   :dialog="dialog"
                   :acount-data="customerData"
                   @close="callback">
     <template v-slot:footer>
       <div style="float:right;">
         <!--        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>-->
         <el-button @click="callback">取消</el-button>
         <el-button @click="adoptHandleClick" type="success">通过</el-button>
         <el-button @click="refuseHandleClick" type="danger">拒绝</el-button>
       </div>
     </template>
   </CustomerDetail>
</template>

<script>
import CustomerDetail from './CustomerDetail'
export default {
  name: 'AuditCustomer',
  props: {
    dialog: Boolean,
    customerData: {},
    callback: Function
  },
  components: {
    CustomerDetail
  }

}
</script>

这样组件B就可以根据的需要对组件A页面进行部分覆写。

各组件的数据传递 view-组件B-组件A

  页面添加组件A
  <AuditCustomer ref="customerAuditDetail"
                 :dialog="isShowAudit"
                 :customer-data="currentEditCustomerData"
                 :callback="auditCloseCallBack"
                 @refuse="auditrefuseCallBack"
                 @adopt="auditAdoptCallBack"
  >
  </AuditCustomer>
  组件B-组件A
<CustomerDetail ref="customerDetail"
                   :dialog="dialog"
                   :acount-data="customerData"
                   @close="callback">
  </CustomerDetail>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值