關於Vue中:key="index"的console警告


在寫vue項目時,瀏覽器的console出現如下警告信息:

[Vue warn]: Property or method "index" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties. found in ---> <Navheader> at E:\GitRepository\companyProject\src\components\crimalcheck\crimalcheck.vue <App> at E:\GitRepository\companyProject\src\App.vue <Root>

原因很簡單:不熟悉或者粗心的情況下很容易把v-for循環寫成如下形式

<td v-for="item in xxx" :key="index" >
 
正確的寫法應為<td v-for="(item,index) in xxx" :key="index" >
 
ps:把:key="index"刪掉也可以解決此警告,但是這樣就違反了Vue文檔當中的推薦寫法,v-for當中最好還是把:key寫上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM