1 回答

TA貢獻1818條經(jīng)驗 獲得超7個贊
先寫下發(fā)現(xiàn)的問題吧
computed是可以依賴vuex中數(shù)據(jù)并獲取數(shù)據(jù)的。
return this.$store.state.products改為return this.$store.state.products.products就可以了。
至于為什么多了一層。是因為modules,分了模塊的會將模塊內(nèi)的state放在state[模塊名]這個對象里
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})
store.state.a // -> moduleA 的狀態(tài)
store.state.b // -> moduleB 的狀態(tài)
可以看到。moduleA的狀態(tài)全在store.state.a這個對象中。
你的問題
為什么沒更新,因為獲取到的是一個對象。{products:[]},沒有title等屬性,無法渲染,當然直接輸出{{product}}還是能看到更新后的數(shù)據(jù)的。
為什么使用了getters就可以正確獲取state了呢,因為getters默認獲取的是同modules內(nèi)的數(shù)據(jù)。
那我估計你對派生的理解也沒有疑問了。好比computed的作用,我們用watch和methods配合state也可以做到,為什么要用computed?因為使用computed有明確的依賴關系。
添加回答
舉報