vue 封裝el-table組件,使用v-bind="$props" 和 $listeners

項(xiàng)目中想基于element 中el-talbe 在做一層封裝,這樣做的好處是

  • 對el-table 組件可控
  • 基于el-table 組件做定制化開發(fā)

想要實(shí)現(xiàn)組件層級如下:

page 組件,業(yè)務(wù)層代碼
my-table 組件,自己封裝的組件,中間層代理組件,需要開發(fā)
el-talbe 組件,基于element-ui 的table組件

image.png

開發(fā)思路

開發(fā)邏輯是,在page頁面組件中,引入 my-table 組件,傳遞屬性prop到 my-table 組件,my-table 組件傳遞屬性prop 到 el-table組件。

數(shù)據(jù)傳遞

my-table需要實(shí)現(xiàn)el-table 組件的所有prop,event 傳遞。示意圖如下:


image.png

事件event傳遞

并且el-table 中所有的emit 事件,都需要在my-table 做一層轉(zhuǎn)發(fā)到 page 組件。


image.png

my-table 需要做到似有似無到效果,但要做到可攔截,并且做數(shù)據(jù)格式處理,樣式定制化。

初級實(shí)現(xiàn)

page組件,所有prop都是el-table 的配置

<page 
    :data="data" 
    :height ="100"
    :max-height="200" 
    :stripe ="false"
    :border="false"
    :size="'small'"
    ...
>
</page>

my-table 做代理轉(zhuǎn)發(fā)prop 到el-table

<my-table>
   <el-table
    :data="data" 
    :height ="100"
    :max-height="200" 
    :stripe ="false"
    :border="false"
    :size="'small'"
    ...
  >
  </el-table>
</my-table>
<script>
props: {
  data,
  height,
  maxHeight,
  stripe,
  border,
  size
   ...
}
</script>

高級實(shí)現(xiàn)

page組件,所有prop都是el-table 的配置

<page 
    :data="data" 
    :height ="100"
    :max-height="200" 
    :stripe ="false"
    :border="false"
    :size="'small'"
    ...
>
</page>

my-table 使用v-bind="$props"做事件event代理轉(zhuǎn)發(fā)到el-table

<my-table>
   <el-table
    v-bind="$props"
  >
  </el-table>
</my-table>
<script>
</script>

my-table 使用v-on="$listeners"做代理轉(zhuǎn)發(fā)prop 到el-table

<my-table>
   <el-table
    v-bind="$props"
    v-on="$listeners"
  >
  </el-table>
</my-table>
<script>
</script>

如何實(shí)現(xiàn)自定義,添加分頁組件

處理Table-column Attributes

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容