admin管理员组文章数量:1415458
I have the following simple v-for loop:
<div class="HolderRow" v-for="(row, index) in 9" :key="index">
<Holder v-for="(row, index) in 11" :key="index" :holder1="increment()" />
</div>
On every render of "Holder", I wish to increment a counter by 1 and pass it as a prop to Holder. The increment method along with the incrementing data is as follows:
export default {
data() {
return {
show: false,
holder: 0
};
},
methods: {
increment() {
this.holder = this.holder + 1;
return this.holder;
}
},
ponents: {
Holder
}
But, the problem is I get the following warning from Vue:
vue.runtime.esm.js?2b0e:619 [Vue warn]: You may have an infinite update loop in a ponent render function.
Is there any way to implement this without incurring such a warning/error?
Thanks
I have the following simple v-for loop:
<div class="HolderRow" v-for="(row, index) in 9" :key="index">
<Holder v-for="(row, index) in 11" :key="index" :holder1="increment()" />
</div>
On every render of "Holder", I wish to increment a counter by 1 and pass it as a prop to Holder. The increment method along with the incrementing data is as follows:
export default {
data() {
return {
show: false,
holder: 0
};
},
methods: {
increment() {
this.holder = this.holder + 1;
return this.holder;
}
},
ponents: {
Holder
}
But, the problem is I get the following warning from Vue:
vue.runtime.esm.js?2b0e:619 [Vue warn]: You may have an infinite update loop in a ponent render function.
Is there any way to implement this without incurring such a warning/error?
Thanks
Share Improve this question asked Apr 3, 2020 at 16:39 MathiasMathias 6051 gold badge9 silver badges19 bronze badges 1-
see
:holder1="index1+index2"
instead of a function, also you userow
andindex
in nested loops....bad practice – depperm Commented Apr 3, 2020 at 16:42
3 Answers
Reset to default 2This works (not sure if there is any other way):
<div class="HolderRow" v-for="(row, rowindex) in 9" :key="rowindex">
<Holder
v-for="(holder, holderindex) in 11"
:key="holderindex"
:holder="holder + (11 * rowindex)"
/>
</div>
Remove brackets of increment.
Should be :holder1="increment"
If you use brackets, increment function will be called before event is triggered.
First issue is you are using the same variable names in your loops, I would suggest using an alternate naming scheme. Then instead of mutating holder
in a function it looks like you just want an index so you could do index1*9+index2
<div class="HolderRow" v-for="(row1, index1) in 9" :key="index1">
<Holder v-for="(row2, index2) in 11" :key="index2" :holder1="index1*9+index2" />
</div>
本文标签:
版权声明:本文标题:javascript - How can I increment a counter in Vue JS from within a v-for loop without incurring an infinite update loop warning? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745165738a2645665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论