Vuex 模块可以监视其他模块的状态,并因此触发动作吗?
例如,让我们考虑以下情况:
store.js
import time from './store/time' ;
import position from './store/position' ;
const store = new Vuex.Store
(
{
modules:
{
time,
position
}
}
) ;
store/time.js
export default
{
namespaced: true,
state:
{
time: new Date()
},
getters:
{
time: (aState) => aState.time
},
mutations:
{
setTime (aState, aTime)
{
aState.time = aTime ;
}
}
} ;
store/position.js
export default
{
namespaced: true,
state:
{
positions: {}
},
getters:
{
positions: aState => aState.positions
},
mutations:
{
setPositions (aState, aPositionArray)
{
aState.positions = aPositionArray ;
}
},
actions:
{
fetchPositionsAtTime ({ dispatch, commit, rootGetters }, { time })
{
// Fetch positions at given time from the server
// commit('setPositions', ...)
}
}
} ;
理想情况下,我希望位置模块监视时间模块,并fetchPositionsAtTime
在时间状态更改后立即重新获取位置(即 trigger )。
当然,我可以向setTime
突变添加调度以触发位置模块操作,但我相信反过来(即观看)会更优雅(因为更多模块可能需要时间)。
有什么解决办法吗?(当然不使用Vue Component
,这就是重点)