vue中組件參數(shù)的示例分析-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比休寧縣網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式休寧縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋休寧縣地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。

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

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

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

我們來看下最為簡單和常見的vue代碼

<div id="root">
      <item content="hello"></item>
    </div>
    <script>
      Vue.component("item",{
        props:["content"],
        template:"<div>{{content}}</div>"
      })
      new Vue({
        el:"#root"
      })
    </script>

這是一個最簡單的創(chuàng)建組件和父組件向子組件的例子,但是我們在是否可以考慮一下,如果我希望父組件向子組件傳遞參數(shù)的時候是個數(shù)字類型呢?又或者是布爾類型呢?所以我們在這里就必須要對父組件傳遞過來的參數(shù)做一個校驗。

<div id="root">
      <item content="hello"></item>
    </div>
    <script>
      Vue.component("item",{
        props:{
          content:String
        },
        template:"<div>{{content}}</div>"
      })
      new Vue({
        el:"#root"
      })
    </script>

我們對第一個例子的代碼進行了修改,我們把子組件中的props屬性,改為一種對象的形式,而且我們也約束了父組件傳遞過來的content為String類型,但是還會有這樣的一種情況出現(xiàn),請看下面的代碼:

<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ù)字類型,那么頁面就會出現(xiàn)報錯的提示.但是我們打開頁面后,并沒有瀏覽器報錯。這又是為什么呢?

在vue中,默認傳遞的值都是字符串,如果你想要傳遞一個數(shù)字,那么必須在content前面添加一個:

我們希望它出現(xiàn)報錯,那么我們就應該這么修改以上的代碼。

<div id="root">
      <item :content="1"></item>
    </div>
    <script>
      Vue.component("item",{
        props:{
          content:String
        },
        template:"<div>{{content}}</div>"
      })
      new Vue({
        el:"#root"
      })
    </script>

那么這個時候,VUE就會給我們一個代碼錯誤提示。如果我們希望它不報錯,那么我們修改一下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>

當然了,content也是可以接受一個數(shù)組的,用來判斷它父組件為子組件傳遞的多個參數(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ù)組形式,我們也可以寫成對象的形式。那么對象的形式,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>

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

本文名稱:vue中組件參數(shù)的示例分析-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://muchs.cn/article2/peooc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站外貿(mào)網(wǎng)站建設、營銷型網(wǎng)站建設App開發(fā)、軟件開發(fā)、動態(tài)網(wǎng)站

廣告

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

網(wǎng)站優(yōu)化排名