解決vue props傳Array/Object類型值,子組件報(bào)錯(cuò)的情況
問題:
Props with type Object/Array must use a factory function to return the default value.
1、在vue中如果當(dāng)在父組件通過props傳Array/Object類型值給子組件的時(shí)候
2、如果子組件的props接收default為 ,如下

報(bào)錯(cuò)

原因:props default 數(shù)組/對象的默認(rèn)值應(yīng)當(dāng)由一個(gè)工廠函數(shù)返回
解決:

補(bǔ)充知識:vue的props如何傳多個(gè)參數(shù)
vue父作用域?qū)?shù)據(jù)傳到子組件通過props進(jìn)行傳參,如果需要傳多個(gè)參數(shù)可以數(shù)組形式賦值給props,通過這樣子組件就可以獲取父組件傳體過來的多個(gè)參數(shù)了。
<div id='app'> <ul > <todo-item v-for='(item,index) in arr' v-bind:todo='item' v-bind:index='index'></todo-item> </ul></div><script type='text/javascript' src='http://www.b3g6.com/bcjs/js/vue.js' ></script><script> Vue.component('todo-item',{ props:[’todo’,’index’], template:'<li>{{index}}:{{todo.text}}</li>' }) var app = new Vue({ el:'#app', data:{ arr: [ { text: ’學(xué)習(xí) JavaScript’ }, { text: ’學(xué)習(xí) Vue’ }, { text: ’整個(gè)牛項(xiàng)目’ } ] } })</script>
以上這篇解決vue props傳Array/Object類型值,子組件報(bào)錯(cuò)的情況就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

網(wǎng)公網(wǎng)安備