我們來聊一下vue中的組件參數(shù).
1.vue中組件參數(shù)
我們可以為組件的 prop 指定驗(yàn)證要求,例如你知道的這些類型。如果有一個(gè)需求沒有被滿足,則 Vue 會(huì)在瀏覽器控制臺(tái)中警告你。這在開發(fā)一個(gè)會(huì)被別人用到的組件時(shí)尤其有幫助。
我們來看下最為簡(jiǎn)單和常見的vue代碼
<div id="root">      <item content="hello"></item>    </div>    <script>      Vue.component("item",{        props:["content"],        template:"<div>{{content}}</div>"      })      new Vue({        el:"#root"      })    </script>這是一個(gè)最簡(jiǎn)單的創(chuàng)建組件和父組件向子組件的例子,但是我們?cè)谑欠窨梢钥紤]一下,如果我希望父組件向子組件傳遞參數(shù)的時(shí)候是個(gè)數(shù)字類型呢?又或者是布爾類型呢?所以我們?cè)谶@里就必須要對(duì)父組件傳遞過來的參數(shù)做一個(gè)校驗(yàn)。
<div id="root">      <item content="hello"></item>    </div>    <script>      Vue.component("item",{        props:{          content:String        },        template:"<div>{{content}}</div>"      })      new Vue({        el:"#root"      })    </script>我們對(duì)第一個(gè)例子的代碼進(jìn)行了修改,我們把子組件中的props屬性,改為一種對(duì)象的形式,而且我們也約束了父組件傳遞過來的content為String類型,但是還會(huì)有這樣的一種情況出現(xiàn),請(qǐng)看下面的代碼:
<div id="root">      <item content="1"></item>    </div>    <script>      Vue.component("item",{        props:{          content:String        },        template:"<div>{{content}}</div>"      })      new Vue({        el:"#root"      })    </script>我們改變了父組件中content的值等于1,那么我們就很自然的把content理解為數(shù)字類型,那么頁(yè)面就會(huì)出現(xiàn)報(bào)錯(cuò)的提示.但是我們打開頁(yè)面后,并沒有瀏覽器報(bào)錯(cuò)。這又是為什么呢?
在vue中,默認(rèn)傳遞的值都是字符串,如果你想要傳遞一個(gè)數(shù)字,那么必須在content前面添加一個(gè):
我們希望它出現(xiàn)報(bào)錯(cuò),那么我們就應(yīng)該這么修改以上的代碼。
<div id="root">      <item :content="1"></item>    </div>    <script>      Vue.component("item",{        props:{          content:String        },        template:"<div>{{content}}</div>"      })      new Vue({        el:"#root"      })    </script>那么這個(gè)時(shí)候,VUE就會(huì)給我們一個(gè)代碼錯(cuò)誤提示。如果我們希望它不報(bào)錯(cuò),那么我們修改一下content里面的類型
<div id="root">      <item :content="1"></item>    </div>    <script>      Vue.component("item",{        props:{          content:Number        },        template:"<div>{{content}}</div>"      })      new Vue({        el:"#root"      })    </script>當(dāng)然了,content也是可以接受一個(gè)數(shù)組的,用來判斷它父組件為子組件傳遞的多個(gè)參數(shù)。
<div id="root">      <item :content="1"></item>    </div>    <script>      Vue.component("item",{        props:{          content:[String,Number]        },        template:"<div>{{content}}</div>"      })      new Vue({        el:"#root"      })    </script>除了數(shù)組形式,我們也可以寫成對(duì)象的形式。那么對(duì)象的形式,vue為我們提供了各種可選的參數(shù)。
<div id="root">      <item content="hello world"></item>    </div>    <script>      Vue.component("item",{        props:{          content:{            type:String,            required:true,            default:"asd",            validator:function(value){              return (value.length>5)            }          }        },        template:"<div>{{content}}</div>"      })      new Vue({        el:"#root"      })    </script>總結(jié)
以上所述是小編給大家介紹的vue中組件參數(shù),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,
小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注