【Vue】slot和slot-scope

官方文檔

即插槽,插槽定義在子組件內(nèi),是組件的一塊HTML模板,可以在子組件內(nèi)設(shè)置默認(rèn)值,也可以在父組件內(nèi)定義插槽內(nèi)顯示的內(nèi)容

默認(rèn)插槽,具名插槽和作用域插槽


舉個例子:

<!-- child組件 -->
<template>
  <section>
      <!-- 具名插槽 -->
      <slot name="header">
          <header>這是子組件內(nèi)定義header</header>
      </slot>
      <!-- 匿名插槽 -->
      <slot></slot>
      <!-- 具名插槽 -->
      <slot name="footer">
          <footer>這是子組件內(nèi)定義footer</footer>
      </slot>
  </section>
</template>

<!-- 父組件內(nèi)使用 -->
<template>
  <div>
      <child>
          <div>父組件內(nèi)定義內(nèi)容</div>
          <footer slot="footer">父組件內(nèi)定義插槽內(nèi)容</footer>
      </child>
  </div>
</template>

最終顯示效果

<slot name="header"><slot name="footer">是具名插槽,在父組件使用時,在<child>標(biāo)簽內(nèi)定義任意標(biāo)簽,并添加屬性slot = “name”,則該標(biāo)簽就會覆蓋組件內(nèi)對用的插槽內(nèi)容。
<slot>是匿名插槽,不設(shè)置name屬性,<child>標(biāo)簽內(nèi),不被具名插槽包裹的內(nèi)容,會覆蓋匿名插槽的內(nèi)容。

  <!-- child組件 -->
  <ul>
    <li  v-for="todo in todos"  :key="todo.id">
      <slot :todo="todo">
        <!-- 回退的內(nèi)容 -->
        {{ todo.text }}
      </slot>
    </li>
  </ul>
  <!-- 使用 -->
  <child :todos="todos">
    <!-- 將 `slotProps` 定義為插槽作用域的名字 -->
    <template slot-scope="slotProps">
      <span v-if="slotProps.todo.isComplete">?</span>
        {{ slotProps.todo.text }}
    </template>
  </child>

子組件內(nèi),定義slot,綁定todo屬性,在父組件內(nèi)使用時,在child內(nèi),通過slot-scope接受一個對象參數(shù),可以根據(jù)參數(shù),定制每個代辦項(xiàng)。slot-scope="{todo}"可以結(jié)構(gòu)獲取參數(shù)??墒嵌x多個作用域插槽。

針對ElementUI框架Table組件的二次封裝

    <!-- 二次封裝  z-table  -->
  <div>
    <el-table :data="rows"
              v-loading="loading"
              v-bind="$attrs"
              v-on="$listeners"
              ref="table"
              empty-text="沒有滿足條件的數(shù)據(jù)哦!">
      <!-- 公共column等等。 -->
      <el-table-column v-for="col in columns"
                       :key="col.colName"
                       :label="col.viewName"
                       :width="col.width"
                       :prop="col.colName"
                       show-overflow-tooltip>
        <!-- 通過slot-scope獲取el-table定義的row -->
        <template slot-scope="{row}">
          <!-- 將row和col屬性,通過作用域插槽暴露給父組件 -->
          <slot :col="col" :row="row">
            {{row[col.colName]}}
          </slot>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination等其他公共設(shè)置 -->
  <div>
  <!-- 使用 -->
  <z-table :rows="rows" :columns="cols" :loading="loading" checked :page="pages"  @changePage="pageChange">
          <template slot-scope="{col,row}">
            <div v-if="true">
              <!-- todo -->
            </div>
            <div v-else>
              {{row[col.colName]}}
            </div>
          </template>
        </z-table>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 什么是組件? 組件 (Component) 是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML 元素,封裝...
    youins閱讀 9,708評論 0 13
  • 組件(Component)是Vue.js最核心的功能,也是整個架構(gòu)設(shè)計最精彩的地方,當(dāng)然也是最難掌握的。...
    六個周閱讀 5,775評論 0 32
  • 序言:亂七八糟一鍋粥! 基于Vue.js 教程、介紹— Vue.js 心得: 在vue中,推薦始終使用 kebab...
    苦苦修行閱讀 697評論 0 1
  • 一直這么認(rèn)為自己,可也確實(shí)如此。 想了一大段責(zé)罵自己的話,卻怎么也寫不出來 故事發(fā)生在很久以前 那時你還是積極向...
    muramasa_閱讀 363評論 0 1
  • 嗨!歡迎來到清慧讀書,這里是清慧的心靈花園,每個人都有屬于自己的心靈花園,你可曾想過走進(jìn)它嗎?清慧愿意通過讀書筆記...
    般雅閱讀 1,651評論 0 0

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