Vue props中Object和Array設(shè)置默認(rèn)值操作
我就廢話不多說(shuō),看代碼吧~
seller: { type: Object, default() { return {} }}
seller: { type: Object, default: function () { return {} }}
當(dāng)父組件沒(méi)有傳這個(gè)值或者值是空時(shí),輸出的話,返回:
下面這種是錯(cuò)誤的
seller: { type: Object, default: () => {}}
當(dāng)父組件沒(méi)有傳這個(gè)值或者值是空時(shí),輸出的話,這時(shí)是返回underfind,在template中獲取里面的值時(shí),就報(bào)錯(cuò)。
補(bǔ)充知識(shí):解決vue props 默認(rèn)值為數(shù)組或?qū)ο髸r(shí)報(bào)錯(cuò)的問(wèn)題
簡(jiǎn)單粗暴直接上代碼:
// 這些寫(xiě)時(shí)會(huì)報(bào)錯(cuò) Type of the default value for ’record’ prop must be a function props: { record: { type: Array, default: [] } } // 正確應(yīng)該這樣寫(xiě)// 因?yàn)関ue規(guī)定,對(duì)象或數(shù)組默認(rèn)值必須從一個(gè)工廠函數(shù)獲取 props: { record: { type: Array, default: function () { return [] } }}
以上這篇Vue props中Object和Array設(shè)置默認(rèn)值操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 利用單元測(cè)試對(duì)PHP代碼進(jìn)行檢查2. python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML3. python excel和yaml文件的讀取封裝4. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決5. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊6. python3實(shí)現(xiàn)往mysql中插入datetime類(lèi)型的數(shù)據(jù)7. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析8. 如何對(duì)php程序中的常見(jiàn)漏洞進(jìn)行攻擊9. js獲取今天、昨天、明天的日期函數(shù)代碼10. Python 實(shí)現(xiàn)二叉查找樹(shù)的示例代碼
