Vue method changing data used as v-if
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.
javascript vue.js vuejs2
add a comment |
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.
javascript vue.js vuejs2
add a comment |
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.
javascript vue.js vuejs2
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
javascript vue.js vuejs2
asked Nov 13 '18 at 8:57
SergiSergi
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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?
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
add a comment |
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
});
}
});
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%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
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?
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
add a comment |
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?
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
add a comment |
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?
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?
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
add a comment |
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
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.
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%2f53277184%2fvue-method-changing-data-used-as-v-if%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