鍍金池/ 問(wèn)答/HTML/ vue.js v-for每次循環(huán)兩條項(xiàng)目,里面的處理函數(shù)會(huì)出現(xiàn)重復(fù)調(diào)用的問(wèn)題

vue.js v-for每次循環(huán)兩條項(xiàng)目,里面的處理函數(shù)會(huì)出現(xiàn)重復(fù)調(diào)用的問(wèn)題

我現(xiàn)在要實(shí)現(xiàn)的是v-for每次按2條項(xiàng)目這樣來(lái)循環(huán),里面有函數(shù)是處理index,可是會(huì)出現(xiàn)符合的index重復(fù)調(diào)用的情況。
template:
clipboard.png
scrip:

<script>
let count = 3
let indexArr = []
export default {
  name: 'reportPage',
  data () {
    return {
        tdata: [1,2,3],
        myForm: {
              'myCompany': '海南凱迪網(wǎng)絡(luò)資訊股份有限公司',
              'myTips': ''
        },
        totalPage: 4,
        
    }
  },
  mounted () {
      let that = this
      let height = document.body.clientHeight - 60
    that.$refs.main.style.height = height+'px'
//    alert(((that.tdata.length - 1)%2))
    
    if (that.tdata.length === 1 ) {
        that.totalPage = 4
    }else {
        that.totalPage = 4 + Math.floor((that.tdata.length - 1)/2) + 1
    }
//    alert(that.totalPage)
//    if ((that.tdata.length%2) && tdata.length>1) {
//        that.totalPage = 3 + that.tdata.length + 1
//    }else{
//        that.totalPage = 3 + that.tdata.length
//    }
  },
  methods: {
      submitForm (e) {
          let that = this
        e.preventDefault()
        that.form.validateFields((err, values) => {
            if (!err) {
                   console.log('Received values of form: ', values);
            }
          })
      },
      textChange () {
          let that = this
//          console.log('tips==',that.form.getFieldValue('tips'))
          that.myForm.myTips = that.form.getFieldValue('tips')
      },
      getPageNum (index) {
          console.log("index=", index)
//          console.log("count1=",count)
//          if (indexArr.indexOf(index) == -1) {
//              indexArr.push(index)
//              count--
//          }
//          console.log("count2==",count)
//          console.log('sum=', index + count)
//          return index + count
      }
  }
}
</script>

打印的結(jié)果:

clipboard.png
有誰(shuí)遇到同樣的問(wèn)題嗎?我想要的結(jié)果是打印出來(lái)的index不是重復(fù)執(zhí)行的,就是執(zhí)行一次。

回答
編輯回答
兔囡囡

找到問(wèn)題了,原因是應(yīng)為vue的render使然的,應(yīng)為我在mouted里改變了totalPage導(dǎo)致重新渲染了p,而p里面有用到的函數(shù),所以又執(zhí)行了一次,把totalPage的設(shè)置改為在created里就可以了。

2018年4月3日 17:44
編輯回答
孤慣

把該段提取出來(lái)單獨(dú)驗(yàn)證排除其他部分邏輯問(wèn)題引起的

2018年5月28日 01:53