v-for and v-if not working together when data gets empty array
up vote
0
down vote
favorite
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.
javascript vue.js vuejs2
add a comment |
up vote
0
down vote
favorite
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.
javascript vue.js vuejs2
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
javascript vue.js vuejs2
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.
javascript vue.js vuejs2
javascript vue.js vuejs2
asked Nov 11 at 16:28
Bhojendra Rauniyar
50.2k1978121
50.2k1978121
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
It's not recommended to use v-for with v-if . see official docs: https://vuejs.org/v2/guide/conditional.html#v-if-with-v-for
Thanks @KentV, I was forgetting about this because I came back to vue after a long time. And seeing an issue in github also didn't provide this guide but there's working solution they found I think was old.
– Bhojendra Rauniyar
Nov 11 at 16:44
add a comment |
up vote
0
down vote
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 component.
Though, we agree from the documentation states: Never use v-if on the same element as v-for.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It's not recommended to use v-for with v-if . see official docs: https://vuejs.org/v2/guide/conditional.html#v-if-with-v-for
Thanks @KentV, I was forgetting about this because I came back to vue after a long time. And seeing an issue in github also didn't provide this guide but there's working solution they found I think was old.
– Bhojendra Rauniyar
Nov 11 at 16:44
add a comment |
up vote
1
down vote
accepted
It's not recommended to use v-for with v-if . see official docs: https://vuejs.org/v2/guide/conditional.html#v-if-with-v-for
Thanks @KentV, I was forgetting about this because I came back to vue after a long time. And seeing an issue in github also didn't provide this guide but there's working solution they found I think was old.
– Bhojendra Rauniyar
Nov 11 at 16:44
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It's not recommended to use v-for with v-if . see official docs: https://vuejs.org/v2/guide/conditional.html#v-if-with-v-for
It's not recommended to use v-for with v-if . see official docs: https://vuejs.org/v2/guide/conditional.html#v-if-with-v-for
answered Nov 11 at 16:32
Kent V
432316
432316
Thanks @KentV, I was forgetting about this because I came back to vue after a long time. And seeing an issue in github also didn't provide this guide but there's working solution they found I think was old.
– Bhojendra Rauniyar
Nov 11 at 16:44
add a comment |
Thanks @KentV, I was forgetting about this because I came back to vue after a long time. And seeing an issue in github also didn't provide this guide but there's working solution they found I think was old.
– Bhojendra Rauniyar
Nov 11 at 16:44
Thanks @KentV, I was forgetting about this because I came back to vue after a long time. And seeing an issue in github also didn't provide this guide but there's working solution they found I think was old.
– Bhojendra Rauniyar
Nov 11 at 16:44
Thanks @KentV, I was forgetting about this because I came back to vue after a long time. And seeing an issue in github also didn't provide this guide but there's working solution they found I think was old.
– Bhojendra Rauniyar
Nov 11 at 16:44
add a comment |
up vote
0
down vote
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 component.
Though, we agree from the documentation states: Never use v-if on the same element as v-for.
add a comment |
up vote
0
down vote
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 component.
Though, we agree from the documentation states: Never use v-if on the same element as v-for.
add a comment |
up vote
0
down vote
up vote
0
down vote
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 component.
Though, we agree from the documentation states: Never use v-if on the same element as 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 component.
Though, we agree from the documentation states: Never use v-if on the same element as v-for.
edited Nov 11 at 16:39
answered Nov 11 at 16:28
Bhojendra Rauniyar
50.2k1978121
50.2k1978121
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250775%2fv-for-and-v-if-not-working-together-when-data-gets-empty-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown