1. 程式人生 > 程式設計 >vue中提示$index is not defined錯誤的解決方式

vue中提示$index is not defined錯誤的解決方式

今天學習Vue中遇到了一個報錯資訊:$index is not defined,是我寫了個for迴圈在HTML中,然後是因為版本的問題

下面是解決方法:

原來的是 v-for="person in items"

v-on:click="deletePerson($index)"//這個僅僅適用於1.0版本,不要採坑了同學們

這個在Vue1.0版本中式適用的可以直接使用$index,但是在2.0是不適合的

在Vue 2.0版本中獲取索引我們需要通過 v-for = "(person,index) in items ",點選事件我們也不能使用$index,應該使用

v-on:click="deletePerson(index)"

補充知識:vue中滾動頁面,改變樣式&&導航欄滾動時,樣式透明度修改

.vue

<div class="commonHeader" v-bind:class="{ 'navActive': scrollFlag }">

<img src="@/images/home/icon_jdjr.png" v-bind:class="{ 'scrollFlag': scrollFlag }">

data

scrollFlag:false,

mounted

window.addEventListener('scroll',this.handleScroll)

methods

handleScroll () {
 let _this=this;
 var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
 // console.log(scrollTop)
 if(scrollTop){
  _this.scrollFlag=true
 }else{
  _this.scrollFlag=false
 }
}

以上這篇vue中提示$index is not defined錯誤的解決方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。