2017-04-20 87 views
1

如果我有一個計算的屬性,它看起來像這樣:Vue.js如何在計算屬性中設置觀察者?

computed: { 
    greeting() { 
    const state = this.$store.state; 
    if (state.name === 'Joe') { 
     return 'Hey, Joe'; 
    } else { 
     return 'Hello, ' + state.name; 
    } 
    } 
} 

什麼目標(S)爲Vue公司將設置一個觀察者的? this.$store.statestate.name或兩者兼而有之?問是因爲:

  • 我要確保這Vue的實例沒有監聽任何和所有更改Vuex店 - 這似乎是可能在一個大的應用程序性能下降。
  • 我有一個業務需求,通過道具設置一些計算屬性,我很難讓它們被動。
  • 我只是很好奇。
+1

這應該胳肢你的想象。 https://www.skyronic.com/blog/vuejs-internals-computed-properties –

回答

0

前提是this.$store.statethis.$store.state.name性質是反應性的(它們應該是),那麼將Vue公司觀察改變這些屬性和重新評估greeting當這些屬性的值而改變。

沒有其他屬性會被觀察到。

這將導致greeting進行重新評估:

this.$store.state.name = 'foo'; 
this.$store.state = bar(); 

這將原因greeting進行重新評估:

this.$store.state.foo = 'foo'; 
this.$store.state.a.b.c = 'bar'; 
this.$store.apple = 'pink lady';