Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么这是无限循环?_Javascript_Vue.js - Fatal编程技术网

Javascript 为什么这是无限循环?

Javascript 为什么这是无限循环?,javascript,vue.js,Javascript,Vue.js,我有一个“基本”函数,它检查数组中的[I]元素是否与id相同: checkArray(offer, id){ if (id) { this.count=0; for (var i in offer.specialities) { if (offer.specialities[i] == id) { consol

我有一个“基本”函数,它检查数组中的[I]元素是否与id相同:

    checkArray(offer, id){
        if (id)
        {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id)
                {
                  console.log("bam!")
                  // this.count=+1;
                  return true;
                  break;
                } 
            }
            return false;
        }
        else {
            return true;
        }
    },
变量计数在vuejs数据中声明

data() {
  return {
    count: 0 
  } 
}
从v-show调用checkArray:

<v-layout  row wrap v-for="offer in offers.slice((current_page-1)*5, current_page*5)" 
v-show="checkArray(offer, speciality)">

为什么会这样?如何计算变量中BAM的数量?

Vue认为您有一个无限循环,因为您在同一个循环中读取和修改了count变量

因为您在循环中读取了变量count,所以vue将开始监视count变量的任何更新

因为您编写了count变量,所以vue将在下一个滴答声中重新运行每个侦听器

您应该将循环体的计算委托给单独的计算属性

currentPageView() {
    return this.offers.slice((current_page-1)*5, current_page*5);
},

shownPageView() {
    const result = [];
    for(let i = 0; i < currentPageView.length; i++) {
        const offer = currentPageView[i];
        const id = this.speciality;
        if (id) {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id) {
                  result.push(offer);
                  break;
                } 
            }
        } else {
             result.push(offer);
        }
    }
    return result;
},

countSpecialOffers() {
    let count = 0;
    for(let i = 0; i < currentPageView.length; i++) {
        const offer = currentPageView[i];
        const id = this.speciality;
        if (id) {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id) {
                  count++;
                  break;
                } 
            }
        }
    }
    return count;
}
currentPageView(){
返回此.offers.slice((当前页面-1)*5,当前页面*5);
},
shownPageView(){
常量结果=[];
for(设i=0;i

完成此操作后,您可以访问
shownPageView
循环查看结果,也可以访问
countSpecialLoffers
获取特价商品的数量

这不是无限循环,而是语法错误。您不能为(offer.specialities中的i=0)设置
。你可以为(我在报价中。特价)设置
,但不能为
=0
我在报价中设置=0。特价
该怎么办??@JonasW.:如果是,则需要两个
checkArray
所在的位置called@gileneusz您在哪里调用
checkArray
。。。您能否提供代码的
模板
部分?最可能发生的情况是,在
checkArray
方法中使用
this.count
会一次又一次地改变状态,导致组件每次都重新渲染。这就是答案,我不知道为什么会发生这种情况
currentPageView() {
    return this.offers.slice((current_page-1)*5, current_page*5);
},

shownPageView() {
    const result = [];
    for(let i = 0; i < currentPageView.length; i++) {
        const offer = currentPageView[i];
        const id = this.speciality;
        if (id) {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id) {
                  result.push(offer);
                  break;
                } 
            }
        } else {
             result.push(offer);
        }
    }
    return result;
},

countSpecialOffers() {
    let count = 0;
    for(let i = 0; i < currentPageView.length; i++) {
        const offer = currentPageView[i];
        const id = this.speciality;
        if (id) {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id) {
                  count++;
                  break;
                } 
            }
        }
    }
    return count;
}