Vue-Slot 插槽詳解

Vue-Slot 插槽詳解

什么是插槽

定義

實現(xiàn)功能

  • 將自定義內(nèi)容插入 組件中指定的位置

自定義內(nèi)容 (包括不限于以下)

  • HTML代碼
  • 文本內(nèi)容
  • 組件 ..

插槽的作用

  • 父組件,子組件的狀態(tài)數(shù)據(jù)傳遞 (作用域插槽)
  • 提供靈活的,私人訂制版組件

插槽的分類

默認插槽

代碼

  • //定義組件 Card 
    <template>
      <div class="cardContainer">
        <h3>Card組件</h3>
        <slot>我是一個插槽</slot>
      </div>
    </template>
    
    /****************************************/
    
    //父組件使用Card組件
    <template>
      <div id="app">
        //使用Card組件
        <Card>
          <template>
            //模板中的內(nèi)容會替換 Card 組件中 <slot></slot>內(nèi)容
          </template>
        </Card>
      </div>
    </template>   
    

具名插槽

代碼

  • //Vue解析模版時,默認情況下,未指定名稱的插槽默認值為default
    //同一個組件中,存在多個未命名插槽,當僅使用一個時,會將內(nèi)容插入所有插槽中
    
    //定義具名插槽
    <template>
      <div class="cardContainer">
        <slot name="content"> 我是插槽1 </slot>
        <h4>更多內(nèi)容 敬請關(guān)注...</h4>
        <slot name="footer">我是個插槽2</slot>
      </div>
    </template>
    
    
    //使用具名插槽
    <template>
      <div id="app">
        <Card title="電影">
          <!--  v-slot:插槽名 -->
          <template v-slot:content>
            我在使用名稱:content的插槽
          </template>
        </Card>
      </div>
    </template>
    

作用域插槽

使用場景

  • 父組件在向插槽中插入內(nèi)容時需要使用子組件中定義的數(shù)據(jù)時

  • //作用域插槽 子組件
    <template>
      <div class="cardContainer">
        <h3>{{ title }}</h3>
        <slot name="content" :content="content"> 我是插槽1 </slot>
        <!-- 設(shè)置一個插槽 -->
        <h4>更多內(nèi)容 敬請關(guān)注...</h4>
        <slot name="footer">我是個插槽2</slot>
        <span></span>
      </div>
    </template>
    <script>
    export default {
      name: "Card",
      data() {
        return {};
      },
      props: {
        title: String,
      },
      computed: {
        content() {
          if (this.title === "電影") {
            return ["恐怖", "驚悚", "喜劇"];
          }
          if (this.title === "游戲") {
            return ["單機", "聯(lián)機"];
          }
        },
      },
    };
    </script>
    <style scoped>
    .cardContainer {
      width: 400px;
      margin-left: 20px;
      background-color: aquamarine;
    }
    h3 {
      text-align: center;
    
      background-color: bisque;
    }
    </style>
    
    
  • 父組件

  • <template>
      <div id="app">
        <Card title="電影">
          <!-- v-slot:插槽名="插槽屬性綁定值 ,類型 Object" -->
          <template v-slot:content="contentInfo">
            <ul>
              <li v-for="item in contentInfo.content" :key="item">{{ item }}</li>
            </ul>
          </template>
        </Card>
        <Card title="游戲">
          <template v-slot:content>
            <ol>
              <li>單機</li>
              <li>聯(lián)機</li>
            </ol>
          </template>
        </Card>
      </div>
    </template>
    
    <script>
    import Card from "./components/slot組件/Card";
    
    export default {
      name: "App",
      components: {
        Card,
      },
      mounted() {},
    };
    </script>
    
    <style>
    #app {
      display: flex;
      justify-content: space-around;
    }
    </style>
    
    

注意事項

  • 父級模板里的所有內(nèi)容都是在父級作用域中編譯的;子模板里的所有內(nèi)容都是在子作用域中編譯的。
  • v-slot 指令的使用區(qū)別與其他指令的語法形式
    • v-if= “”
    • v-slot:插槽名 (vue @2.6+版本新語法)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

  • vue插槽slot 一、前言 vue官方文檔中在"組件基礎(chǔ)"內(nèi)容中提到組件可以通過插槽分發(fā)內(nèi)容,那插槽是怎么使用的...
    追尋1989閱讀 594評論 0 2
  • slot基本使用 在子組件中,使用特殊的元素 就可以為子組件開啟一個插槽。該插槽插入什么內(nèi)容取決于父組件如何使用。...
    獨調(diào)1997閱讀 6,960評論 4 4
  • 插槽分類:具名插槽與作用域插槽。在新版中(2.6.0)統(tǒng)一都叫v-slot指令,老版中稱之為slot與slot-s...
    小話001閱讀 2,087評論 0 0
  • 為什么使用插槽: 因為封裝組件的時候一些功能有區(qū)別,但是也有很多共性,這時候需要預留一些slot,使用的時候填內(nèi)容...
    畢方_Fang閱讀 1,268評論 0 1
  • 1、什么是插槽 VUE官方文檔的解釋: Vue 實現(xiàn)了一套內(nèi)容分發(fā)的 API,將 <slot> 元素作為承載分發(fā)內(nèi)...
    五花漏Z閱讀 36,767評論 3 26

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