欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在Vue利用slot插槽分發(fā)內(nèi)容

本篇文章給大家分享的是有關(guān)如何在Vue利用slot插槽分發(fā)內(nèi)容,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

我們提供的服務(wù)有:成都網(wǎng)站制作、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、莒縣ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的莒縣網(wǎng)站制作公司

單個(gè)插槽:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="vue.min.js"></script>
  </head>
  <body>
    <div id="app">
      <h2>我是父組件的標(biāo)題</h2>
      <my-component>
        <p>這是一些初始內(nèi)容</p>
        <p>這是更多的初始內(nèi)容</p>
      </my-component>
    </div>
    <script type="text/javascript">
      Vue.component('my-component', {
       // 有效,因?yàn)槭窃谡_的作用域內(nèi)
       template: '<div>\
              <h3>我是子組件的標(biāo)題</h3>\
              <slot>只有在沒(méi)有要分發(fā)的內(nèi)容時(shí)才會(huì)顯示。</slot>\
            </div>',
       data: function () {
        return {
         
        }
       }
      });
      new Vue({
        el:'#app',
        data:{
          msg:'你好啊'
        }
      })

    </script>
  </body>
</html>

組件中的模板中寫入slot標(biāo)簽,在父級(jí)調(diào)用子組件的時(shí)候傳入html即可。

具名插槽:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="vue.min.js"></script>
  </head>
  <body>
    <div id="app">
      <my-component>
         <h2 slot="header">這里可能是一個(gè)頁(yè)面標(biāo)題</h2>

         <p>主要內(nèi)容的一個(gè)段落。</p>
         <p>另一個(gè)主要段落。</p>

         <p slot="footer">這里有一些聯(lián)系信息</p>
      </my-component>
    </div>
    <script type="text/javascript">
      Vue.component('my-component', {
       // 有效,因?yàn)槭窃谡_的作用域內(nèi)
       template: '<div class="container">\
             <header>\
              <slot name="header"></slot>\
             </header>\
              <main>\
              <slot></slot>\
             </main>\
             <footer>\
              <slot name="footer"></slot>\
             </footer>\
            </div>',
       data: function () {
        return {
         
        }
       }
      });
      new Vue({
        el:'#app',
        data:{
          msg:'你好啊'
        }
      })

    </script>
  </body>
</html>

具名插槽,顧名思義當(dāng)有多個(gè)slot標(biāo)簽時(shí),你需要給他們起個(gè)自己的名字,在父組件調(diào)用時(shí)需要slot="內(nèi)部的對(duì)應(yīng)名字",當(dāng)存在沒(méi)有命名的slot標(biāo)簽時(shí),父級(jí)組件傳入的默認(rèn)html代碼將全部輸出在無(wú)名的slot標(biāo)簽中。

作用域插槽

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="vue.min.js"></script>
  </head>
  <body>
    <div id="app">
       <!-- 在父級(jí)中,具有特殊特性 slot-scope 的 <template> 元素必須存在,表示它是作用域插槽的模板。slot-scope 的值將被用作一個(gè)臨時(shí)變量名,此變量接收從子組件傳遞過(guò)來(lái)的 prop 對(duì)象: -->
       <child>
        <template scope="props">
         <span>hello from parent</span><br>
         <span>{{ props.text }}</span>
        </template>
      </child>
    </div>
    <script type="text/javascript">
      // 在子組件中,只需將數(shù)據(jù)傳遞到插槽,就像你將 prop 傳遞給組件一樣:
      Vue.component('child',{
        template:'<ul>' +
              '<slot text="hello from child"></slot>' +
             '</ul>',
        data:function(){
         return {

         }
        },
      });
      new Vue({
        el:'#app',
        data:{
          msg:'你好啊'
        }
      })

    </script>
  </body>
</html>

作用域插槽是一種特殊類型的插槽,用作一個(gè) (能被傳遞數(shù)據(jù)的) 可重用模板,來(lái)代替已經(jīng)渲染好的元素。

其實(shí)就是相當(dāng)于,在父組件中可以獲取到子組件傳遞出來(lái)的props對(duì)象,從而渲染到父組件上。

以上就是如何在Vue利用slot插槽分發(fā)內(nèi)容,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:如何在Vue利用slot插槽分發(fā)內(nèi)容
網(wǎng)站URL:http://chinadenli.net/article28/ggjicp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器電子商務(wù)標(biāo)簽優(yōu)化移動(dòng)網(wǎng)站建設(shè)網(wǎng)頁(yè)設(shè)計(jì)公司網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)