Vue props中Object和Array設(shè)置默認(rèn)值操作
我就廢話不多說,看代碼吧~
seller: { type: Object, default() { return {} }}
seller: { type: Object, default: function () { return {} }}
當(dāng)父組件沒有傳這個(gè)值或者值是空時(shí),輸出的話,返回:
下面這種是錯(cuò)誤的
seller: { type: Object, default: () => {}}
當(dāng)父組件沒有傳這個(gè)值或者值是空時(shí),輸出的話,這時(shí)是返回underfind,在template中獲取里面的值時(shí),就報(bào)錯(cuò)。
補(bǔ)充知識:解決vue props 默認(rèn)值為數(shù)組或?qū)ο髸r(shí)報(bào)錯(cuò)的問題
簡單粗暴直接上代碼:
// 這些寫時(shí)會報(bào)錯(cuò) Type of the default value for ’record’ prop must be a function props: { record: { type: Array, default: [] } } // 正確應(yīng)該這樣寫// 因?yàn)関ue規(guī)定,對象或數(shù)組默認(rèn)值必須從一個(gè)工廠函數(shù)獲取 props: { record: { type: Array, default: function () { return [] } }}
以上這篇Vue props中Object和Array設(shè)置默認(rèn)值操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
