vue中組件參數(shù)的示例分析

這篇文章給大家分享的是有關(guān)vue中組件參數(shù)的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供大埔網(wǎng)站建設(shè)、大埔做網(wǎng)站、大埔網(wǎng)站設(shè)計(jì)、大埔網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、大埔企業(yè)網(wǎng)站模板建站服務(wù),10年大埔做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

1.vue中組件參數(shù)

我們可以為組件的 prop 指定驗(yàn)證要求,例如你知道的這些類型。如果有一個(gè)需求沒有被滿足,則 Vue 會(huì)在瀏覽器控制臺(tái)中警告你。這在開發(fā)一個(gè)會(huì)被別人用到的組件時(shí)尤其有幫助。

我們來(lái)看下最為簡(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ì)父組件傳遞過(guò)來(lái)的參數(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ì)象的形式,而且我們也約束了父組件傳遞過(guò)來(lái)的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ù)組的,用來(lái)判斷它父組件為子組件傳遞的多個(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>

感謝各位的閱讀!關(guān)于“vue中組件參數(shù)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

當(dāng)前文章:vue中組件參數(shù)的示例分析
網(wǎng)站網(wǎng)址:http://muchs.cn/article32/ghjepc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、Google、定制網(wǎng)站、ChatGPT服務(wù)器托管、電子商務(wù)

廣告

聲明:本網(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)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)