vuejs slot 怎麼使用

2021-09-24 16:01:50

vuejs slot的使用方法:1、在子元件內放一些DOM;2、通過slot實現顯示或者隱藏DOM,程式碼如「new Vue({el: "#app",data: {},components:{children:{...}}}) 」。

本文操作環境:Windows7系統、vue2.9.6版,DELL G3電腦。

vuejs中slot的使用

概述:

假如父元件需要在子元件內放一些DOM,那麼這些DOM是顯示或者隱藏,在哪個地方顯示,怎麼顯示,需要slot分發負責。

分以下幾種情況分發:


<p class="" id="app">
    <children>
      <span>我是slot內容</span>   <!--這行不會顯示-->
    </children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
      },
      components: {
        children: { //這個無返回值,不會繼續派發          
        template: "<button>為了明確作用範圍,所以使用button標籤</button>"
        }
      }
    })  </script>

顯示結果為:span標籤內的內容並沒有顯示。

單個slot:


<p class="" id="app">
    <children>
      <span>我是slot內容</span>
      <!--這行不會顯示-->
    </children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
      },
      components: {
        children: { //這個無返回值,不會繼續派發          
        template: "<button><slot></slot>為了明確作用範圍,所以使用button標籤</button>"
        }
      }
    })  </script>


即父元件放在子元件裡的內容,插到了子元件<slot></slot>位置;
注意:即使有多個標籤,會一起被插入,相當於在父元件放在子元件裡的標籤,替換了<slot></slot>這個標籤。
例如:<p class="" id="app">
    <children>
      <span>我是slot內容</span>
      <p>測試測試</p>
      <em>我是測試的</em>
      <!--這行不會顯示-->
    </children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {

      },
      components: {
        children: { //這個無返回值,不會繼續派發          template: "<button><slot></slot>為了明確作用範圍,所以使用button標籤</button>"
        }
      }
    })  </script>

d42ac6bb2e2d5996319d78d4ba6013c.png

D:具名slot:將放在子元件裡的不同html標籤放到不同位置,父元件在要釋出的標籤裡新增slot=」name名」屬性,子元件在對應分發的位置的slot標籤裡,新增name=」name名」屬性,然後就會將對應的標籤放在對應位置了。
例如:<p class="" id="app">
    <children>
      <span slot="first">12345</span>
      <span slot="third">56789</span>
    </children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
      },
      components: {
        children: { //這個無返回值,不會繼續派發          template: "<button><slot name='first'></slot>為了明確作用範圍<slot name='third'></slot>所以使用button標籤</button>"
        }
      }
    })  </script>

a989fdd6a742def4b099852dec74ed1.png

E:分發內容的作用域:
被分發的內容的作用域,根據其所在模組決定,例如:<p class="" id="app">
    <children>
      <span slot="first" @click="test()">12345</span>
      <span slot="third">56789</span>
    </children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
      },
      components: {
        children: { //這個無返回值,不會繼續派發          template: "<button><slot name='first'></slot>為了明確作用範圍<slot name='third'></slot>所以使用button標籤</button>"
        }
      },
      methods: {
        test: function() {
          console.log("我是first點選列印的內容");
        }
      }
    })  </script>點選按鈕12345的區域時(而不是全部按鈕),會觸發父元件的test方法,然後列印「我是first點選列印的內容」。但是點選其他區域則沒有影響。

e3fd307f05128cf982620be0646344b.png

F:當沒有分發內容時的提示:假如父元件沒有在子元件中放置有標籤,或者是父元件在子元件中放置標籤,但有slot屬性,而子元件中沒有slot屬性的標籤。那麼子元件的slot標籤,將不會起任何作用。除非,該slot標籤內有內容,那麼在無分發內容的時候,會顯示該slot標籤內的內容。
例如:<p class="" id="app">
    <children>
      <span slot="last">【12345】</span>
    </children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
      },
      components: {
        children: { //這個無返回值,不會繼續派發          template: "<p><slot name='first'><button>【沒內容顯示1】</button></slot>為了明確作用範圍,<slot name='last'><button>【沒內容顯示2】</button></slot>所以使用button標籤</p>"
        }
      }
    })  </script>

f5f9fc710840715c10c3aacbcbcdeb9.png

如果改為:<p class="" id="app">
    <children>
      <span slot="first">【12345】</span>
    </children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
      },
      components: {
        children: { //這個無返回值,不會繼續派發          template: "<p><slot name='first'><button>【沒內容顯示1】</button></slot>為了明確作用範圍,<slot name='last'><button>【沒內容顯示2】</button></slot>所以使用button標籤</p>"
        }
      }
    })  </script>

3c1f3c2e83c2ae690d8087f74326aaa.png

說明:a、name=」first」的slot標籤被父元件對應的標籤所替換(slot標籤內部的內容被捨棄)
b、name=」last」的slot標籤,因為沒有對應內容,則顯示該slot標籤內部的內容。

G、假如想控制子元件根標籤的屬性【1】首先,由於模板標籤是屬於父元件的,因此,將子元件的指令繫結在模板標籤裡,是不可以的(因為其歸屬於父元件)
【2】假如需要通過父元件控制子元件是否顯示(例如v-show或v-if),那麼這個指令顯然是屬於父元件的,可以將標籤寫在子元件的模板上。例如:<p class="" id="app">
    <button @click="toshow">點選讓子元件顯示</button>
    <children v-if="abc"></children>
  </p>
  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
        abc: false
      },
      methods: {
        toshow: function() {          this.abc = !this.abc;
        }
      },
      components: {
        children: { //這個無返回值,不會繼續派發          template: "<p>這裡是子元件</p>"
        }
      }
    })  </script>

380092f445e519ebba677786800e9c5.png

點選之後:

30414c28a05a6792b21cfb6e7f158b9.png

說明:通過父元件(點選按鈕,切換v-if指令的值)控制子元件是否顯示。
【3】假如需要通過子元件,控制子元件是否顯示(比如隱藏)那麼這個指令顯然是屬於子元件的(會將值放在子元件的data屬性下)那麼就必須放置在子元件的根標籤中。
例如:<p class="" id="app">
    <button @click="toshow">點選讓子元件顯示</button>
    <children>
      <span slot="first">【12345】</span>
      <!--上面這行不會顯示-->
    </children>
  </p>

  <script type="text/javascript">
    new Vue({
      el: "#app",
      methods: {
        toshow: function() {          this.$children[0].tohidden = true;
        }
      },
      components: {
        children: { //這個無返回值,不會繼續派發          template: "<p v-if='tohidden' @click='tohide'>這裡是子元件</p>",
          data: function() {            return {
              tohidden: true
            }
          },
          methods: {
            tohide: function() {              this.tohidden = !this.tohidden;
            }
          }
        }
      }
    })  </script>

4498f5d2f0b9660888b26e5ae079f5a.png

點選「這裡是子元件」之後:

02fecb61b580c78a08f68ffdb7bc0ca.png

點選「點選讓子元件顯示」:
說明:

fd0e70cb808b2230138818e1378f41a.png點選子元件會讓子元件消失
點選父元件的按鈕,通過更改子元件的tohidden屬性,讓子元件重新顯示。
子元件的指令繫結在子元件的模板之中(如此才能呼叫)。

推薦學習:《》

以上就是vuejs slot 怎麼使用的詳細內容,更多請關注TW511.COM其它相關文章!