我們希望在不同頁面之間共享值。我們通過以下方式進行了嘗試。
在同一頁面上,State 回傳了正確的值,但在不同的頁面上,未設定 State。我們做錯了什么嗎?還是 Vuex Store 不能在頁面之間共享值?
對于這個問題,我們已經用簡化的代碼替換了它們。導致問題的代碼更復雜。
/store/index.js
const NOT_SET = null
export const state = () => ({
value: NOT_SET,
})
export const mutations = {
setValue(state, value) {
state.value = value
},
}
/pages/index.vue
<script>
import { mapState, mapMutations } from 'vuex'
export default {
computed: {
...mapState(['value']),
},
mounted() {
this.setValue(100)
},
methods: {
...mapMutations(['setValue']),
},
}
</script>
/pages/_XXX/index.vue
<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState(['value']),
},
}
</script>
uj5u.com熱心網友回復:
您不能使用a標簽,因為它們每次都會擦除整個 SPA。如果您在 Vue 背景關系中,則需要使用 Vue 路由器,因此nuxt-link如果您想在整個程序中保留應用程式,則這是強制性的。
它還將大大提高整個網站的性能。
因為否則,Vuex 完全能夠在所有頁面之間共享狀態。實際上,它的目的是在整個 Vue 應用程式中共享它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/473067.html
