vue中父子組件注意事項,傳值及slot應(yīng)用技巧

一.父子組件傳值

創(chuàng)新互聯(lián)是一家專業(yè)提供永定企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站制作、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為永定眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進(jìn)行中。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>父子組件傳值</title>
  <style> 
  </style>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="root"> 
    <counter :count="0" @numberchange="handleChange"></counter>
    <counter :count="0" @numberchange="handleChange"></counter>
    <div>{{total}}</div> 
    <validate-content content="hello world"></validate-content>
  </div>
  <script> 
   //父組件向子組件傳值用 props ,加:號后傳遞的為js表達(dá)式,示例中則為數(shù)字,不加:號代表的是字符串 
   var counter = { //局部注冊 
     props:['count'],
     data:function(){//在子組件中定義數(shù)據(jù),data不能是對象,必須是一個函數(shù)。
       return {
         number:this.count
       }
     },
     template:'<div @click="handleClick2">{{number}}</div>',
     methods:{
      handleClick2:function(){
        this.number ++;
        //this.count++; 父組件可以傳值給子組件,但子組件不可以修改父組件屬性,這里這么寫會報錯。
        this.$emit("numberchange",this.number);//子組件向父組件傳遞事件,值
      }
    } 
   }
   var validateContent = {
    props:{
      //content:[Number,String] //組件參數(shù)校驗,可以多選
      content:{//組件參數(shù)校驗
        type:String,
        required:true,
        default:"default value",
        validator:function(value){
          return value.length > 5
        }
      }
     },
     template:'<div >{{content}}</div>',
   }
   var vm = new Vue({
     el:'#root',
     data:{
       total:0
     },
     methods:{ 
      handleChange:function(number){ 
        console.log(number)
        // this.total +=1;
      }
     },
     components:{
       counter, //局部注冊要在根節(jié)點注冊組件
       validateContent
     }
   })
  </script>
</body>
</html>

二.父組件向子組件傳遞DOM

先看一個示例

<body>
  <div id="root"> 
    <child><p>Qin</p></child>
  </div>
  <script> 
   let child = {
     template :`<div>
           <p>hello world</p> 
        </div>`
   }
   var vm = new Vue({
     el:'#root',
     components:{
       child
     } 
   })
  </script>
</body>

打開查看器查看一下

vue中父子組件注意事項,傳值及slot應(yīng)用技巧

發(fā)現(xiàn)Qin不見了

<p>Qin</p>1

查看官方文檔 ,   https://cn.vuejs.org/v2/guide/components-slots.html 

我們得出結(jié)論:如果 child 沒有包含一個 < slot > 元素,則任何傳入它的內(nèi)容都會被拋棄

 我們加入插槽

<body>
  <div id="root"> 
    <child><p>Qin</p></child>
  </div>
  <script> 
   let child = {
     template :`<div>
           <p>hello world</p>
           <slot></slot>
        </div>` 
   }
   var vm = new Vue({
     el:'#root',
     components:{
       child
     } 
   })
  </script>
</body>

發(fā)現(xiàn)Qin能正常顯示,且slot將會被替換為解析后的片段 < p > Qin < /p >

vue中父子組件注意事項,傳值及slot應(yīng)用技巧

當(dāng)父組件不向子組件傳值的時候,slot還可以作為父組件默認(rèn)值出現(xiàn)

<body>
  <div id="root"> 
    <child></child>
  </div>
  <script> 
   let child = {
     template :`<div>
           <p>hello world</p>
           <slot>default value</slot>
        </div>`
   }
   var vm = new Vue({
     el:'#root',
     components:{
       child
     } 
   })
  </script>
</body>

效果圖

vue中父子組件注意事項,傳值及slot應(yīng)用技巧

具名插槽

 如果想使用多個插槽,我們先看看效果:

<body>
  <div id="root"> 
    <child>
      <header>This is header</header>
      <footer>This is footer</footer> 
    </child>
  </div>
  <script> 
   let child = {
     template :
       `<div> 
           <slot></slot>
           <p>Main content</p>
           <slot></slot>
        </div>`
   }
   var vm = new Vue({
     el:'#root',
     components:{
       child
     } 
   })
  </script>
</body>

vue中父子組件注意事項,傳值及slot應(yīng)用技巧

發(fā)現(xiàn)出現(xiàn)了多個header和footer,要解決這個問題就要用到具名插槽  

我們修改代碼如下:

<body>
  <div id="root"> 
    <child>
      <header slot="header">This is header</header>
      <footer slot="footer">This is footer</footer> 
    </child>
  </div>
  <script> 
   let child = {
     template :
       `<div> 
           <slot name="header"></slot>
           <p>Main content</p>
           <slot name="footer"></slot>
        </div>`
   }
   var vm = new Vue({
     el:'#root',
     components:{
       child
     } 
   })
  </script>
</body>

vue中父子組件注意事項,傳值及slot應(yīng)用技巧 

可以看到顯示正常了

總結(jié)

以上所述是小編給大家介紹的vue中父子組件注意事項,傳值及slot應(yīng)用技巧,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

當(dāng)前標(biāo)題:vue中父子組件注意事項,傳值及slot應(yīng)用技巧
文章出自:http://bm7419.com/article30/pcihpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站云服務(wù)器、網(wǎng)站制作軟件開發(fā)、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化

廣告

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

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