admin管理员组文章数量:1327095
I faced a really silly problem today and just sharing it here:
v-for and v-if doesn't work together when its data value is []
. For eg:
ts: []
<div v-for="t in ts" :key="t" v-if="ts.length">
Yes
</div>
<div v-else>
No
</div>
See it in action here in codesandbox.
We can see nothing is rendered and even no error is thrown. I also tried with v-if="ts.length > 0"
but it still renders nothing.
I faced a really silly problem today and just sharing it here:
v-for and v-if doesn't work together when its data value is []
. For eg:
ts: []
<div v-for="t in ts" :key="t" v-if="ts.length">
Yes
</div>
<div v-else>
No
</div>
See it in action here in codesandbox.
We can see nothing is rendered and even no error is thrown. I also tried with v-if="ts.length > 0"
but it still renders nothing.
2 Answers
Reset to default 3It's not remended to use v-for with v-if . see official docs: https://v2.vuejs/v2/guide/conditional.html#v-if-with-v-for
One possible solution is to separate v-if
from v-for
like this:
<div v-if="ts.length">
<div v-for="t in ts" :key="t">
Yes
</div>
</div>
<div v-else>
No
</div>
But this seems to be bad solution for me while others can use it as a good solution. Because I was needed to use v-for
and v-if
together. Its because I don't like to separate pieces from file and just need to use inside table's tr ponent.
Though, we agree from the documentation states: Never use v-if on the same element as v-for.
本文标签: javascriptvfor and vif not working together when data gets empty arrayStack Overflow
版权声明:本文标题:javascript - v-for and v-if not working together when data gets empty array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206853a2432979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论