使用VUEX如何实现更新浏览器不同关卡的内容?
如下面DEMO,如何实现在page1更新count后,page2不用刷新自动更新?
store/index.js
import { createStore } from 'vuex'
const store = createStore({
state: {
count: 3
},
mutations: {
add(state) {
state.count += 1
}
},
getters: {
returnCount: state => {
return state.count;
},
},
})
export default store
page1
<view @click="addCount">{{ count }}</view>
computed: {
count() {
return this.$store.state.count;
},
},
methods: {
addCount() {
store.commit("add");
}
}
page2
<view>{{ count }}</view>
computed: {
count() {
return this.$store.getters.returnCount
}
}
同域名下,可以用 localStorage,也可以用 service worker。