由於 Vuex
的狀態存儲本來就是響應式的,從 store
實例中讀取狀態最簡單的方法,就是在計算屬性
中返回某個狀態。
在 B 頁面
引入以下代碼:
computed: {
myValue() {
return this.$store.state.someValue } }
此時,當 A 頁面
通過某種方式更改了 this.$store.state.someValue
的值,B 頁面
中 myValue
的值便會自動得到更新。
如果題主所指的監聽,是要在這個值變化后觸發其他動作,則需要在 B 頁面
加入偵聽屬性:
watch: {
myValue: function(newVal, oldVal) { //其他業務代碼 } }