gpt4 book ai didi

javascript - VueJS : Why counter does not increase on post increment but works on pre-increment

转载 作者:行者123 更新时间:2023-11-30 11:19:37 27 4
gpt4 key购买 nike

this fiddle ,为什么计数器在调用 increment() 函数时不增加。已将计数器设置为增加对自身的分配后分配,因此在单击事件按钮(多次)后,计数器变量应该增加。

  • 注意:如果我像这样进行预增量而不是后增量,this.counter =++this.counter; 就可以了。

HTML

<div id="app">
<h2>some text</h2>
<p>{{ counter }}</p>
<button @click="increment">Increment</button>
</div>

VueJS

new Vue({
el: "#app",
data: {
counter: 0
},
methods: {
increment: function(){
this.counter = this.counter++;
}
}
})

最佳答案

这与 VueJS 无关。这是一个简单的前缀/后缀问题。看这段代码:

var test = 0;
console.log(test++);
console.log(++test);

此外,您可以将此行 this.counter = this.counter++; 缩短为 this.counter++,因为 this.counter++;this.counter = this.counter + 1;

的简写

关于javascript - VueJS : Why counter does not increase on post increment but works on pre-increment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50288645/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com