admin管理员组文章数量:1277885
I have a button where I want it to add 1 to the amount of ponents whenever it is clicked. However, the displayed value did not change but when I type in the variable in the console, it is updated.
<div x-data="addItem()">
<button @click="addItem()">+ 1</button>
<br>
<span x-text="amountOfComponents"></span>
<br>
<span x-text="itemPrice"></span>
</div>
var amountOfComponents = 0;
var itemPrice = 0;
const addItem = () => {
amountOfComponents += 1;
if (amountOfComponents <= 5) {
itemPrice = 500 + (110 * amountOfComponents)
} else if (amountOfComponents > 5, amountOfComponents <= 10) {
itemPrice = 1000 + (105 * amountOfComponents)
} else if (amountOfComponents > 10) {
itemPrice = 1500 + (90 * amountOfComponents)
}
return {
amountOfComponents,
itemPrice
}
}
Also, how can I run it so that it shows 0 as the initial value? Pardon for my lack of knowledge in JavaScript.
I have a button where I want it to add 1 to the amount of ponents whenever it is clicked. However, the displayed value did not change but when I type in the variable in the console, it is updated.
https://codepen.io/reonLOW/pen/ExyGyKb
<div x-data="addItem()">
<button @click="addItem()">+ 1</button>
<br>
<span x-text="amountOfComponents"></span>
<br>
<span x-text="itemPrice"></span>
</div>
var amountOfComponents = 0;
var itemPrice = 0;
const addItem = () => {
amountOfComponents += 1;
if (amountOfComponents <= 5) {
itemPrice = 500 + (110 * amountOfComponents)
} else if (amountOfComponents > 5, amountOfComponents <= 10) {
itemPrice = 1000 + (105 * amountOfComponents)
} else if (amountOfComponents > 10) {
itemPrice = 1500 + (90 * amountOfComponents)
}
return {
amountOfComponents,
itemPrice
}
}
Also, how can I run it so that it shows 0 as the initial value? Pardon for my lack of knowledge in JavaScript.
Share Improve this question asked Nov 12, 2020 at 3:56 Reon Low YiyuanReon Low Yiyuan 1532 gold badges2 silver badges10 bronze badges1 Answer
Reset to default 9As AlpineJs documentation states:
x-data declares a new ponent scope. It tells the framework to initialize a new ponent with the following data object.
So, when you when you're returning the modified values, it isn't getting reflected in the ponent object. Also, it's confusing and error-prone to have the same function init object and modify it.
The better approach is to follow AlpineJs ponent approach:
<div x-data="dropdown()">
<button x-on:click="open">Open</button>
<div x-show="isOpen()" x-on:click.away="close">
// Dropdown
</div>
</div>
<script>
function dropdown() {
return {
show: false,
open() { this.show = true },
close() { this.show = false },
isOpen() { return this.show === true },
}
}
</script>
Final code:
const items = () => {
return {
amountOfComponents: 0,
itemPrice: 0,
addItem: function () {
this.amountOfComponents += 1;
if (this.amountOfComponents <= 5) {
this.itemPrice = 500 + (110 * this.amountOfComponents)
} else if (this.amountOfComponents > 5, this.amountOfComponents <= 10) {
this.itemPrice = 1000 + (105 * this.amountOfComponents)
} else if (this.amountOfComponents > 10) {
this.itemPrice = 1500 + (90 * this.amountOfComponents)
}
}
}
}
<script src="https://cdnjs.cloudflare./ajax/libs/alpinejs/2.7.3/alpine.js"></script>
<div x-data="items()">
<button @click="addItem()">+ 1</button>
<br>
<span x-text="amountOfComponents"></span>
<br>
<span x-text="itemPrice"></span>
</div>
本文标签: javascriptHow to update value AlpineJSStack Overflow
版权声明:本文标题:javascript - How to update value AlpineJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741272072a2369487.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论