Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
feat: use global store object in beforeRouteEnter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gem Xli committed Feb 28, 2021
1 parent 2eab71f commit 3ffd458
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
6 changes: 5 additions & 1 deletion client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import routes from 'voie-pages'
import { i18n } from 'vite-i18n-plugin'
import { useI18n } from 'vue-i18n'
import { createStore } from './store'
import { isSSR } from './utils'
import { isSSR, sharedData } from './utils'
import App from './App.vue' // Vue or React main app
import { Router } from 'vue-router'

Expand Down Expand Up @@ -37,4 +37,8 @@ export default viteSSR(App, { routes }, ({ app, initialState, router }) => {
})

app.use(store)

sharedData.$store = store
sharedData.$app = app
sharedData.$router = router
})
51 changes: 33 additions & 18 deletions client/src/pages/archives.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,42 @@
</div>
</template>

<script>
import { mapState } from 'vuex'
<script lang="ts">
import { computed, defineComponent } from 'vue'
import { useUniversalFetch } from '../hooks'
import { useStore } from '../store'
import { useSharedStore } from '../utils'
export default {
async asyncData({ store }) {
await store.dispatch('fetchArchives')
export default defineComponent({
async beforeRouteEnter(to, _, next) {
const store = useSharedStore()
await useUniversalFetch(to, () => store.dispatch('fetchArchives'))
next()
},
computed: {
...mapState(['user', 'menus', 'archives']),
timelineNodes() {
const nodes = this.archives.map((node) => {
return {
title: `${node.year}-${node.month}`,
nodes: node.posts,
}
})
return nodes
},
setup() {
const { state } = useStore()
const archives = computed(() => state.archives)
return {
archives,
user: computed(() => state.user),
menus: computed(() => state.menus),
timelineNodes: computed(() => {
const nodes = (archives.value || []).map((node) => {
return {
title: `${node.year}-${node.month}`,
nodes: node.posts,
}
})
return nodes
}),
}
},
}
})
</script>

<style scoped></style>
2 changes: 2 additions & 0 deletions client/src/utils.ts → client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './sharedData'

export const isSSR = import.meta.env.SSR

export function sleep(ms = 1000) {
Expand Down
8 changes: 8 additions & 0 deletions client/src/utils/sharedData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Store } from 'vuex'
import { ISiteState } from '../store'

export const sharedData: Record<string, any> = {}

export function useSharedStore() {
return sharedData.$store as Store<ISiteState>
}

0 comments on commit 3ffd458

Please sign in to comment.