Vue自定義組件中Props類型為數組或對象


自定義彈框組件時,需要在彈框內顯示要選擇的人員列表,需要組件中的對應字段接收一個Array數組,默認返回空數組[],直接定義空數組報錯,代碼如下:

    props:{
      options: {
        type: Array,
        default: []
      }
    }

提示錯誤如下:

30:9 error Type of the default value for 'options' prop must be a function vue/require-valid-default-prop

✖ 1 problem (1 error, 0 warnings)

錯誤信息提示,不能直接定義空對象的默認值,必須使用 工廠函數 return 回一個默認值。

修改代碼如下,問題解決:

    props:{
      options: {
        type: Array,
        default: () => []
        //default: function () { return [] }
      }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM