Why this computed property is showing changes (Vue)









up vote
1
down vote

favorite












I am changing properties using some buttons. When I click the buttons the data is updated in the UI, even the computed1, but not the console.log("computed1"). I think it's because I am changing its properties and not the entire object. But if it's not triggered, why the UI is updated ? Could you explain me ? I couldn't find something like this in the documentation.



Code: https://jsfiddle.net/1hr7cy5d/






var example1 = new Vue(
el: '#example',
data: function()
return
item:
aaa: 'aaa',
bbb: 'bbb',
ccc: 'ccc'


,
computed:
computed1: function()
console.log("computed1");
let item = this.item
return item;
,
,
methods:
method1()
console.log("method1")
this.item.aaa = 'xxxx';
,
method2()
console.log("method2")
this.item["bbb"] = 'yyyyy';
,
method3()
console.log("method3")
this.$set(this.item, "ccc", "zzzzz")
,
method4() ,

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="example">
<button @click="method1()">method1</button>
<button @click="method2()">method2</button>
<button @click="method3()">method3</button>
<button @click="method4()">method4</button>
<pre>item</pre>
<pre>computed1</pre>
</div>












share|improve this question























  • You're watching for changes on this.item but making changing to this.item.PROP. If you mutate this.item directly then computed1 will run, else, it doesn't see it as a change and won't run. jsfiddle.net/1hr7cy5d/4.
    – Dennis Martinez
    Nov 10 at 17:35











  • but why computed1 is showing changes?
    – eag845
    Nov 10 at 17:38











  • @DennisMartinez but then why does it update the display? Shouldn't it re-run the computed function then if it's updating the display?
    – Derek Pollard
    Nov 10 at 18:04






  • 2




    computed1 isn't returning a new value but a reference to this.item because in javascript objects are by reference, not by value. You're telling Vue to print this.item and you're updating properties of this.item. It won't rerun the function because this.item hasn't changed but rather a property on this.item. Vue doesn't support nested computed properties. You may want to try watch with and option of deep
    – Dennis Martinez
    Nov 10 at 18:16










  • @DennisMartinez you should post as an answer :)
    – Derek Pollard
    Nov 10 at 18:46














up vote
1
down vote

favorite












I am changing properties using some buttons. When I click the buttons the data is updated in the UI, even the computed1, but not the console.log("computed1"). I think it's because I am changing its properties and not the entire object. But if it's not triggered, why the UI is updated ? Could you explain me ? I couldn't find something like this in the documentation.



Code: https://jsfiddle.net/1hr7cy5d/






var example1 = new Vue(
el: '#example',
data: function()
return
item:
aaa: 'aaa',
bbb: 'bbb',
ccc: 'ccc'


,
computed:
computed1: function()
console.log("computed1");
let item = this.item
return item;
,
,
methods:
method1()
console.log("method1")
this.item.aaa = 'xxxx';
,
method2()
console.log("method2")
this.item["bbb"] = 'yyyyy';
,
method3()
console.log("method3")
this.$set(this.item, "ccc", "zzzzz")
,
method4() ,

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="example">
<button @click="method1()">method1</button>
<button @click="method2()">method2</button>
<button @click="method3()">method3</button>
<button @click="method4()">method4</button>
<pre>item</pre>
<pre>computed1</pre>
</div>












share|improve this question























  • You're watching for changes on this.item but making changing to this.item.PROP. If you mutate this.item directly then computed1 will run, else, it doesn't see it as a change and won't run. jsfiddle.net/1hr7cy5d/4.
    – Dennis Martinez
    Nov 10 at 17:35











  • but why computed1 is showing changes?
    – eag845
    Nov 10 at 17:38











  • @DennisMartinez but then why does it update the display? Shouldn't it re-run the computed function then if it's updating the display?
    – Derek Pollard
    Nov 10 at 18:04






  • 2




    computed1 isn't returning a new value but a reference to this.item because in javascript objects are by reference, not by value. You're telling Vue to print this.item and you're updating properties of this.item. It won't rerun the function because this.item hasn't changed but rather a property on this.item. Vue doesn't support nested computed properties. You may want to try watch with and option of deep
    – Dennis Martinez
    Nov 10 at 18:16










  • @DennisMartinez you should post as an answer :)
    – Derek Pollard
    Nov 10 at 18:46












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am changing properties using some buttons. When I click the buttons the data is updated in the UI, even the computed1, but not the console.log("computed1"). I think it's because I am changing its properties and not the entire object. But if it's not triggered, why the UI is updated ? Could you explain me ? I couldn't find something like this in the documentation.



Code: https://jsfiddle.net/1hr7cy5d/






var example1 = new Vue(
el: '#example',
data: function()
return
item:
aaa: 'aaa',
bbb: 'bbb',
ccc: 'ccc'


,
computed:
computed1: function()
console.log("computed1");
let item = this.item
return item;
,
,
methods:
method1()
console.log("method1")
this.item.aaa = 'xxxx';
,
method2()
console.log("method2")
this.item["bbb"] = 'yyyyy';
,
method3()
console.log("method3")
this.$set(this.item, "ccc", "zzzzz")
,
method4() ,

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="example">
<button @click="method1()">method1</button>
<button @click="method2()">method2</button>
<button @click="method3()">method3</button>
<button @click="method4()">method4</button>
<pre>item</pre>
<pre>computed1</pre>
</div>












share|improve this question















I am changing properties using some buttons. When I click the buttons the data is updated in the UI, even the computed1, but not the console.log("computed1"). I think it's because I am changing its properties and not the entire object. But if it's not triggered, why the UI is updated ? Could you explain me ? I couldn't find something like this in the documentation.



Code: https://jsfiddle.net/1hr7cy5d/






var example1 = new Vue(
el: '#example',
data: function()
return
item:
aaa: 'aaa',
bbb: 'bbb',
ccc: 'ccc'


,
computed:
computed1: function()
console.log("computed1");
let item = this.item
return item;
,
,
methods:
method1()
console.log("method1")
this.item.aaa = 'xxxx';
,
method2()
console.log("method2")
this.item["bbb"] = 'yyyyy';
,
method3()
console.log("method3")
this.$set(this.item, "ccc", "zzzzz")
,
method4() ,

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="example">
<button @click="method1()">method1</button>
<button @click="method2()">method2</button>
<button @click="method3()">method3</button>
<button @click="method4()">method4</button>
<pre>item</pre>
<pre>computed1</pre>
</div>








var example1 = new Vue(
el: '#example',
data: function()
return
item:
aaa: 'aaa',
bbb: 'bbb',
ccc: 'ccc'


,
computed:
computed1: function()
console.log("computed1");
let item = this.item
return item;
,
,
methods:
method1()
console.log("method1")
this.item.aaa = 'xxxx';
,
method2()
console.log("method2")
this.item["bbb"] = 'yyyyy';
,
method3()
console.log("method3")
this.$set(this.item, "ccc", "zzzzz")
,
method4() ,

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="example">
<button @click="method1()">method1</button>
<button @click="method2()">method2</button>
<button @click="method3()">method3</button>
<button @click="method4()">method4</button>
<pre>item</pre>
<pre>computed1</pre>
</div>





var example1 = new Vue(
el: '#example',
data: function()
return
item:
aaa: 'aaa',
bbb: 'bbb',
ccc: 'ccc'


,
computed:
computed1: function()
console.log("computed1");
let item = this.item
return item;
,
,
methods:
method1()
console.log("method1")
this.item.aaa = 'xxxx';
,
method2()
console.log("method2")
this.item["bbb"] = 'yyyyy';
,
method3()
console.log("method3")
this.$set(this.item, "ccc", "zzzzz")
,
method4() ,

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="example">
<button @click="method1()">method1</button>
<button @click="method2()">method2</button>
<button @click="method3()">method3</button>
<button @click="method4()">method4</button>
<pre>item</pre>
<pre>computed1</pre>
</div>






javascript vuejs2 computed-properties






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:46

























asked Nov 10 at 17:23









eag845

1698




1698











  • You're watching for changes on this.item but making changing to this.item.PROP. If you mutate this.item directly then computed1 will run, else, it doesn't see it as a change and won't run. jsfiddle.net/1hr7cy5d/4.
    – Dennis Martinez
    Nov 10 at 17:35











  • but why computed1 is showing changes?
    – eag845
    Nov 10 at 17:38











  • @DennisMartinez but then why does it update the display? Shouldn't it re-run the computed function then if it's updating the display?
    – Derek Pollard
    Nov 10 at 18:04






  • 2




    computed1 isn't returning a new value but a reference to this.item because in javascript objects are by reference, not by value. You're telling Vue to print this.item and you're updating properties of this.item. It won't rerun the function because this.item hasn't changed but rather a property on this.item. Vue doesn't support nested computed properties. You may want to try watch with and option of deep
    – Dennis Martinez
    Nov 10 at 18:16










  • @DennisMartinez you should post as an answer :)
    – Derek Pollard
    Nov 10 at 18:46
















  • You're watching for changes on this.item but making changing to this.item.PROP. If you mutate this.item directly then computed1 will run, else, it doesn't see it as a change and won't run. jsfiddle.net/1hr7cy5d/4.
    – Dennis Martinez
    Nov 10 at 17:35











  • but why computed1 is showing changes?
    – eag845
    Nov 10 at 17:38











  • @DennisMartinez but then why does it update the display? Shouldn't it re-run the computed function then if it's updating the display?
    – Derek Pollard
    Nov 10 at 18:04






  • 2




    computed1 isn't returning a new value but a reference to this.item because in javascript objects are by reference, not by value. You're telling Vue to print this.item and you're updating properties of this.item. It won't rerun the function because this.item hasn't changed but rather a property on this.item. Vue doesn't support nested computed properties. You may want to try watch with and option of deep
    – Dennis Martinez
    Nov 10 at 18:16










  • @DennisMartinez you should post as an answer :)
    – Derek Pollard
    Nov 10 at 18:46















You're watching for changes on this.item but making changing to this.item.PROP. If you mutate this.item directly then computed1 will run, else, it doesn't see it as a change and won't run. jsfiddle.net/1hr7cy5d/4.
– Dennis Martinez
Nov 10 at 17:35





You're watching for changes on this.item but making changing to this.item.PROP. If you mutate this.item directly then computed1 will run, else, it doesn't see it as a change and won't run. jsfiddle.net/1hr7cy5d/4.
– Dennis Martinez
Nov 10 at 17:35













but why computed1 is showing changes?
– eag845
Nov 10 at 17:38





but why computed1 is showing changes?
– eag845
Nov 10 at 17:38













@DennisMartinez but then why does it update the display? Shouldn't it re-run the computed function then if it's updating the display?
– Derek Pollard
Nov 10 at 18:04




@DennisMartinez but then why does it update the display? Shouldn't it re-run the computed function then if it's updating the display?
– Derek Pollard
Nov 10 at 18:04




2




2




computed1 isn't returning a new value but a reference to this.item because in javascript objects are by reference, not by value. You're telling Vue to print this.item and you're updating properties of this.item. It won't rerun the function because this.item hasn't changed but rather a property on this.item. Vue doesn't support nested computed properties. You may want to try watch with and option of deep
– Dennis Martinez
Nov 10 at 18:16




computed1 isn't returning a new value but a reference to this.item because in javascript objects are by reference, not by value. You're telling Vue to print this.item and you're updating properties of this.item. It won't rerun the function because this.item hasn't changed but rather a property on this.item. Vue doesn't support nested computed properties. You may want to try watch with and option of deep
– Dennis Martinez
Nov 10 at 18:16












@DennisMartinez you should post as an answer :)
– Derek Pollard
Nov 10 at 18:46




@DennisMartinez you should post as an answer :)
– Derek Pollard
Nov 10 at 18:46

















active

oldest

votes











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%2f53241514%2fwhy-this-computed-property-is-showing-changes-vue%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241514%2fwhy-this-computed-property-is-showing-changes-vue%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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands