国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

詳解vue中組件參數(shù)

2019-11-19 13:31:29
字體:
供稿:網(wǎng)友

我們來聊一下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)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浪卡子县| 永兴县| 怀柔区| 美姑县| 奉新县| 女性| 定南县| 瑞安市| 济源市| 西安市| 西和县| 阿鲁科尔沁旗| 赤城县| 桐城市| 佛坪县| 清水河县| 虎林市| 新竹县| 建水县| 二连浩特市| 马龙县| 封开县| 雅安市| 桑植县| 镇远县| 长宁县| 柳州市| 巴东县| 青海省| 繁昌县| 宜宾市| 广汉市| 九龙坡区| 启东市| 海门市| 婺源县| 垣曲县| 全椒县| 罗甸县| 诸暨市| 沂南县|