[英]How do I use an if statement in a Vue.js function?
What I am trying to do is have the result function offer two different equations:我想做的是让结果 function 提供两个不同的方程:
return
this.number1*(this.number2/100)
if
the {{ basketballbet.odds }}
value is positive if
{{ basketballbet.odds }}
值为正,则return
this.number1*(this.number2/100)
return
this.number1*(100/this.number2)
if
the {{ basketballbet.odds }}
value is negative. if
{{ basketballbet.odds }}
值为负,则return
this.number1*(100/this.number2)
。
Does anyone know if this is possible?有谁知道这是否可能?
code:代码:
<div id="odds_calculator">
<p>Placing <input type="number" name="number1" v-on:input= "update_number1"> on this bet would win you $XXXX if you were to win the bet.</p>
<p>Bet Amount: [[ number1 ]] </p>
<p>Odds: {{ basketballbet.odds }}</p>
<hr>
<p>Result: [[ result() ]]</p>
</div>
<script>
new Vue({
delimiters: ['[[',']]'],
el: '#odds_calculator',
data: {
number1: 0,
number2: {{ basketballbet.odds }},
},
methods: {
update_number1: function (event) {
this.number1 = event.target.value;
},
result: function () {
return this.number1*(this.number2/100);
},
},
});
</script>
Yes, it is.是的。 If it is simply about a positive or negative value then you can use
Math.sign()
.如果它只是一个正值或负值,那么您可以使用
Math.sign()
。 Check here for more info. 在这里查看更多信息。
For example:例如:
if (Math.sign(this.number2) == 1) {
//positive
}else if (Math.sign(this.number2) == -1) {
//negative
}else {
//zero
}
Good luck!祝你好运!
Yes you can but this is also the perfect opportunity to introduce yourself to the ternary operator是的,你可以,但这也是向三元运算符介绍自己的绝佳机会
new Vue({
delimiters: ['[[',']]'],
el: '#odds_calculator',
data: {
number1: 0,
number2: {{ basketballbet.odds }},
},
methods: {
update_number1: function (event) {
this.number1 = event.target.value;
},
result: function () {
//here is the aforementioned ternary operator
return this.number2 < 0 ? this.number1*(100/this.number2) : this.number1*(this.number2/100);
},
},
});
result: function() {
if (this.number1 > 0) {
return this.number1 * (this.number2 / 100);
} else if (this.number1 < 0) {
return this.number1 * (100 / this.number2);
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.