1.問題父組件中使用自定義的子組件hello,現在傳遞了兩個參數:<hello :items="languages" :unit="num"></hello>其中num在父組件的data字段指定具體數值如果不指定:unit="num",子組件內該怎樣判斷是否傳入了相應的props變量unit?2.代碼子組件hello代碼:<template>
</div id="hello">
<ul v=for="item in items">
{{item}}
</ul>
<p>unit:{{unit}}</p>
</div></template><script>export default { props: ['items', 'unit']
}</script>3.解決方法考慮到有些props變量有默認值,有些沒有,需要這樣寫:props: { items: { // 必須提供字段 required: true
}, unit: { // 可選字段,有默認值 default: 3
}
}
vue使用props默認值如何設定
飲歌長嘯
2018-09-06 14:14:33