簡體   English   中英

在子組件中改變數組道具是否有效?

[英]Is it valid to mutate an array prop in a child component?

我注意到,當推送到作為道具傳遞給子組件的數組時,我沒有得到通常的“避免直接改變道具”錯誤。 仔細想想,這是有道理的,因為數組是一個引用,如果某些事情導致父級重新渲染,子級將因此正確渲染。

正確的方法仍然是向父級發出並讓父級對數組進行操作,還是在子組件中對數組進行操作是完全有效的,因為它是一個引用?

根據關於 props 的官方文檔

Note that objects and arrays in JavaScript are passed by reference, so if the prop is an array or object, mutating the object or array itself inside the child component will affect the parent state and Vue is unable to warn you against this. 作為一般規則,您應該避免改變任何道具,包括對象和 arrays,因為這樣做會忽略單向數據綁定並可能導致不希望的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM