简体   繁体   English

'v-if'在添加到'return'函数时不起作用。 为什么?

[英]'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.

相关问题 v-if函数返回true或false时不起作用[VueJs] - v-if Not work when the function return true or false [VueJs] 为什么稍后更新数据时v-if不会受到影响? - Why does v-if not get affected when the data is updated later? 当我将JavaScript块添加到函数中时,该块不起作用。 有人可以帮忙解释原因吗? - A block of JavaScript does not work when I add it to a function. can anybody help explain why? 为什么“v-if”指令在数据更改时不显示隐藏元素 - Why "v-if" directive does not show hidden element when data is changed 为什么在 vue.js 中使用 v-if 时,我的 API 数据显示为未定义? - Why does my API data show up as undefined when using a v-if in vue.js? 当我更改为组合 API 时,v-if 不起作用 - v-if doesn't work when I change to the composition API 更改选项卡时指令 v-if 不起作用 - Directive v-if doesn't work when changing tab Vue:函数在使用索引时不触发模板中的 v-if 元素 - Vue: function not triggering v-if element in template when using index 闭包编译器在我的Javascript函数中添加了“-”。 这是什么意思? - Closure compiler added '--' to my Javascript function. What does it mean? 为什么这个简单的Js函数在定义后添加()时不返回值/对象? - Why does this simple Js function not return a value/object when () is added after the definition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM