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.










share|improve this question


























    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.










    share|improve this question
























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 16:28









      Bhojendra Rauniyar

      50.2k1978121




      50.2k1978121
























          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






          share|improve this answer





















          • 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


















          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.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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






            share|improve this answer





















            • 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















            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






            share|improve this answer





















            • 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













            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






            share|improve this answer












            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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












            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.






            share|improve this answer



























              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.






              share|improve this answer

























                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.






                share|improve this answer














                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 at 16:39

























                answered Nov 11 at 16:28









                Bhojendra Rauniyar

                50.2k1978121




                50.2k1978121






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)