[英]'v-if' does not work when added to the 'return' function. Why?
v-if="css_max_count[i] == true
works fine, but if I add a return
, then this code does not work. Any reasons? v-if="css_max_count[i] == true
工作正常,但如果我添加一个return
,那么这段代码不起作用。有什么理由吗?
short code: 短代码:
<div class="ui form" v-for="(im, i) in sorted_listim">
<div class="ui left pointing red basic label" v-if="css_max_count[i] == true">
{{im.count_buy}}
</div>
<div @click="add_dar(i)" class="ui right small basic green button" ><h3>+</h3>
</div>
</div>
add_dar(i) {
if (im.count_buy < count) {
this.css_max_count[i] = true
// return - ??????
} else this.css_max_count[i] = false
}
When you add a return to the expression, an error occurs because it generates invalid javascript. 向表达式添加返回时,会发生错误,因为它会生成无效的javascript。
The way vue's v-if works is it accepts an expression, which wrapped up in a new Function
object, which contains its own return. vue的v-if工作方式是接受一个表达式,它包含在一个新的Function
对象中,该对象包含自己的返回值。
function checkExpression (exp: string, text: string, warn: Function, range?: Range) {
try {
new Function(`return ${exp}`)
}
...
...
The difference between the two is return css_max_count[i] == true
vs return return css_max_count[i] == true
. 两者之间的区别是return css_max_count[i] == true
vs return return css_max_count[i] == true
。
Adding return
makes an invalid function and an error is thrown. 添加return
会使函数无效并引发错误。 You should have seen an error in your console when this happened because it gets warned out, regardless of environment. 当发生这种情况时,您应该在控制台中看到错误,因为无论环境如何,它都会被警告。
See checkExpression in the vue error-detector for the source. 请参阅vue error-detector中的checkExpression获取源代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.