Vue method changing data used as v-if












0















So what I want to achieve is use a v-if calling a method and inside the method change a property inside data, once this is done then return true so the element is displayed.



Example:






export default {
data() {
return {
lastMonths: [
{
month: '11',
monthNumber: moment().format('MM'),
printed: false
},
{
month: '10',
monthNumber: moment().subtract(1, 'months').format('MM'),
printed: false
},
{
month: '9',
monthNumber: moment().subtract(2, 'months').format('MM'),
printed: false
}
]
}
},
methods: {
displayOptgroup(index, month) {
if (!this.lastMonths[index].printed && (this.lastMonths[index].monthNumber == month)) {
this.lastMonths[index].printed = true;
return true;
}

return false;
},
}
}

<div>
<span class="optgroup" v-if="displayOptgroup(0, option.month)">{{ lastMonths[0].month }}</span>
</div>





But nothing appears, but as soon as I comment the line where I set printed = true, then it works!



I can't understand why this is happening. I tried with computed properties itself, computed properties calling method and mutating there, but with the same result.



Thanks.










share|improve this question



























    0















    So what I want to achieve is use a v-if calling a method and inside the method change a property inside data, once this is done then return true so the element is displayed.



    Example:






    export default {
    data() {
    return {
    lastMonths: [
    {
    month: '11',
    monthNumber: moment().format('MM'),
    printed: false
    },
    {
    month: '10',
    monthNumber: moment().subtract(1, 'months').format('MM'),
    printed: false
    },
    {
    month: '9',
    monthNumber: moment().subtract(2, 'months').format('MM'),
    printed: false
    }
    ]
    }
    },
    methods: {
    displayOptgroup(index, month) {
    if (!this.lastMonths[index].printed && (this.lastMonths[index].monthNumber == month)) {
    this.lastMonths[index].printed = true;
    return true;
    }

    return false;
    },
    }
    }

    <div>
    <span class="optgroup" v-if="displayOptgroup(0, option.month)">{{ lastMonths[0].month }}</span>
    </div>





    But nothing appears, but as soon as I comment the line where I set printed = true, then it works!



    I can't understand why this is happening. I tried with computed properties itself, computed properties calling method and mutating there, but with the same result.



    Thanks.










    share|improve this question

























      0












      0








      0








      So what I want to achieve is use a v-if calling a method and inside the method change a property inside data, once this is done then return true so the element is displayed.



      Example:






      export default {
      data() {
      return {
      lastMonths: [
      {
      month: '11',
      monthNumber: moment().format('MM'),
      printed: false
      },
      {
      month: '10',
      monthNumber: moment().subtract(1, 'months').format('MM'),
      printed: false
      },
      {
      month: '9',
      monthNumber: moment().subtract(2, 'months').format('MM'),
      printed: false
      }
      ]
      }
      },
      methods: {
      displayOptgroup(index, month) {
      if (!this.lastMonths[index].printed && (this.lastMonths[index].monthNumber == month)) {
      this.lastMonths[index].printed = true;
      return true;
      }

      return false;
      },
      }
      }

      <div>
      <span class="optgroup" v-if="displayOptgroup(0, option.month)">{{ lastMonths[0].month }}</span>
      </div>





      But nothing appears, but as soon as I comment the line where I set printed = true, then it works!



      I can't understand why this is happening. I tried with computed properties itself, computed properties calling method and mutating there, but with the same result.



      Thanks.










      share|improve this question














      So what I want to achieve is use a v-if calling a method and inside the method change a property inside data, once this is done then return true so the element is displayed.



      Example:






      export default {
      data() {
      return {
      lastMonths: [
      {
      month: '11',
      monthNumber: moment().format('MM'),
      printed: false
      },
      {
      month: '10',
      monthNumber: moment().subtract(1, 'months').format('MM'),
      printed: false
      },
      {
      month: '9',
      monthNumber: moment().subtract(2, 'months').format('MM'),
      printed: false
      }
      ]
      }
      },
      methods: {
      displayOptgroup(index, month) {
      if (!this.lastMonths[index].printed && (this.lastMonths[index].monthNumber == month)) {
      this.lastMonths[index].printed = true;
      return true;
      }

      return false;
      },
      }
      }

      <div>
      <span class="optgroup" v-if="displayOptgroup(0, option.month)">{{ lastMonths[0].month }}</span>
      </div>





      But nothing appears, but as soon as I comment the line where I set printed = true, then it works!



      I can't understand why this is happening. I tried with computed properties itself, computed properties calling method and mutating there, but with the same result.



      Thanks.






      export default {
      data() {
      return {
      lastMonths: [
      {
      month: '11',
      monthNumber: moment().format('MM'),
      printed: false
      },
      {
      month: '10',
      monthNumber: moment().subtract(1, 'months').format('MM'),
      printed: false
      },
      {
      month: '9',
      monthNumber: moment().subtract(2, 'months').format('MM'),
      printed: false
      }
      ]
      }
      },
      methods: {
      displayOptgroup(index, month) {
      if (!this.lastMonths[index].printed && (this.lastMonths[index].monthNumber == month)) {
      this.lastMonths[index].printed = true;
      return true;
      }

      return false;
      },
      }
      }

      <div>
      <span class="optgroup" v-if="displayOptgroup(0, option.month)">{{ lastMonths[0].month }}</span>
      </div>





      export default {
      data() {
      return {
      lastMonths: [
      {
      month: '11',
      monthNumber: moment().format('MM'),
      printed: false
      },
      {
      month: '10',
      monthNumber: moment().subtract(1, 'months').format('MM'),
      printed: false
      },
      {
      month: '9',
      monthNumber: moment().subtract(2, 'months').format('MM'),
      printed: false
      }
      ]
      }
      },
      methods: {
      displayOptgroup(index, month) {
      if (!this.lastMonths[index].printed && (this.lastMonths[index].monthNumber == month)) {
      this.lastMonths[index].printed = true;
      return true;
      }

      return false;
      },
      }
      }

      <div>
      <span class="optgroup" v-if="displayOptgroup(0, option.month)">{{ lastMonths[0].month }}</span>
      </div>






      javascript vue.js vuejs2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 8:57









      SergiSergi

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          0














          First of all, computed properties are only ment to compute state variables. Methods are the way to go here.
          Have you checked your console? Could you share a little bit more information?






          share|improve this answer
























          • Hey, thanks for your answer. As the example shows I'm using method not computed property. And yes I said I tried with it, but computed was not mutating data itself, instead was calling a method and mutating there. The console shows nothing, no error.

            – Sergi
            Nov 13 '18 at 10:04













          • can you try just returning true in your method. See if it does show your actual DOM element

            – Aria Groult
            Nov 13 '18 at 10:51











          • Hey as I said on the post if I return only true (I said if I comment the line where printed = true, meaning I only return true) the span appear, otherwise not.

            – Sergi
            Nov 13 '18 at 11:12











          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',
          autoActivateHeartbeat: false,
          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%2f53277184%2fvue-method-changing-data-used-as-v-if%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          First of all, computed properties are only ment to compute state variables. Methods are the way to go here.
          Have you checked your console? Could you share a little bit more information?






          share|improve this answer
























          • Hey, thanks for your answer. As the example shows I'm using method not computed property. And yes I said I tried with it, but computed was not mutating data itself, instead was calling a method and mutating there. The console shows nothing, no error.

            – Sergi
            Nov 13 '18 at 10:04













          • can you try just returning true in your method. See if it does show your actual DOM element

            – Aria Groult
            Nov 13 '18 at 10:51











          • Hey as I said on the post if I return only true (I said if I comment the line where printed = true, meaning I only return true) the span appear, otherwise not.

            – Sergi
            Nov 13 '18 at 11:12
















          0














          First of all, computed properties are only ment to compute state variables. Methods are the way to go here.
          Have you checked your console? Could you share a little bit more information?






          share|improve this answer
























          • Hey, thanks for your answer. As the example shows I'm using method not computed property. And yes I said I tried with it, but computed was not mutating data itself, instead was calling a method and mutating there. The console shows nothing, no error.

            – Sergi
            Nov 13 '18 at 10:04













          • can you try just returning true in your method. See if it does show your actual DOM element

            – Aria Groult
            Nov 13 '18 at 10:51











          • Hey as I said on the post if I return only true (I said if I comment the line where printed = true, meaning I only return true) the span appear, otherwise not.

            – Sergi
            Nov 13 '18 at 11:12














          0












          0








          0







          First of all, computed properties are only ment to compute state variables. Methods are the way to go here.
          Have you checked your console? Could you share a little bit more information?






          share|improve this answer













          First of all, computed properties are only ment to compute state variables. Methods are the way to go here.
          Have you checked your console? Could you share a little bit more information?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 9:30









          Aria GroultAria Groult

          1931116




          1931116













          • Hey, thanks for your answer. As the example shows I'm using method not computed property. And yes I said I tried with it, but computed was not mutating data itself, instead was calling a method and mutating there. The console shows nothing, no error.

            – Sergi
            Nov 13 '18 at 10:04













          • can you try just returning true in your method. See if it does show your actual DOM element

            – Aria Groult
            Nov 13 '18 at 10:51











          • Hey as I said on the post if I return only true (I said if I comment the line where printed = true, meaning I only return true) the span appear, otherwise not.

            – Sergi
            Nov 13 '18 at 11:12



















          • Hey, thanks for your answer. As the example shows I'm using method not computed property. And yes I said I tried with it, but computed was not mutating data itself, instead was calling a method and mutating there. The console shows nothing, no error.

            – Sergi
            Nov 13 '18 at 10:04













          • can you try just returning true in your method. See if it does show your actual DOM element

            – Aria Groult
            Nov 13 '18 at 10:51











          • Hey as I said on the post if I return only true (I said if I comment the line where printed = true, meaning I only return true) the span appear, otherwise not.

            – Sergi
            Nov 13 '18 at 11:12

















          Hey, thanks for your answer. As the example shows I'm using method not computed property. And yes I said I tried with it, but computed was not mutating data itself, instead was calling a method and mutating there. The console shows nothing, no error.

          – Sergi
          Nov 13 '18 at 10:04







          Hey, thanks for your answer. As the example shows I'm using method not computed property. And yes I said I tried with it, but computed was not mutating data itself, instead was calling a method and mutating there. The console shows nothing, no error.

          – Sergi
          Nov 13 '18 at 10:04















          can you try just returning true in your method. See if it does show your actual DOM element

          – Aria Groult
          Nov 13 '18 at 10:51





          can you try just returning true in your method. See if it does show your actual DOM element

          – Aria Groult
          Nov 13 '18 at 10:51













          Hey as I said on the post if I return only true (I said if I comment the line where printed = true, meaning I only return true) the span appear, otherwise not.

          – Sergi
          Nov 13 '18 at 11:12





          Hey as I said on the post if I return only true (I said if I comment the line where printed = true, meaning I only return true) the span appear, otherwise not.

          – Sergi
          Nov 13 '18 at 11:12


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277184%2fvue-method-changing-data-used-as-v-if%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

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values