Created
April 28, 2023 02:55
-
-
Save trandaison/1c413587b0a52a582fd2def409a2d6e8 to your computer and use it in GitHub Desktop.
Re-create page component when click on same page router link in Vue/Nuxt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<nuxt-link | |
:to="localizedRoute.fullPath" | |
v-bind="$attrs" | |
v-on="$listeners" | |
@click.native="onClick" | |
> | |
<slot /> | |
</nuxt-link> | |
</template> | |
<script lang="ts"> | |
import Vue, { PropType } from 'vue'; | |
import { mapMutations } from 'vuex'; | |
import { Location, Route } from 'vue-router'; | |
export default Vue.extend({ | |
name: 'AppLink', | |
props: { | |
to: { | |
type: [String, Object] as PropType<string | Location>, | |
default: '', | |
}, | |
localized: { | |
type: Boolean, | |
default: false, | |
}, | |
}, | |
computed: { | |
localizedRoute(): Route | undefined { | |
return this.localized | |
? this.$router.resolve(this.to).resolved | |
: this.localeRoute(this.to); | |
}, | |
isSameRoute(): boolean { | |
return ( | |
!!this.localizedRoute && | |
this.$route.fullPath === this.localizedRoute.fullPath | |
); | |
}, | |
}, | |
methods: { | |
...mapMutations('appLink', ['changeRouterViewKey']), | |
onClick() { | |
if (this.isSameRoute) { | |
this.changeRouterViewKey(); | |
this.$nuxt.refresh(); | |
} | |
}, | |
}, | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="container"> | |
<div class="content-page"> | |
<Nuxt :key="`${routerViewKey}:${$route.fullPath}`" /> | |
</div> | |
<footer class="text-center"> | |
<span>© {{ new Date().getFullYear() }}</span> | |
</footer> | |
</div> | |
</template> | |
<script lang="ts"> | |
import Vue from 'vue'; | |
import { mapState } from 'vuex'; | |
export default Vue.extend({ | |
name: 'DefaultLayout', | |
computed: { | |
...mapState('appLink', ['routerViewKey']), | |
}, | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MutationTree } from 'vuex/types/index'; | |
type AppLinkState = { | |
routerViewKey: string | number; | |
}; | |
export const state = (): AppLinkState => ({ | |
routerViewKey: Date.now(), | |
}); | |
export const mutations: MutationTree<AppLinkState> = { | |
changeRouterViewKey(state, value: string | number = Date.now()) { | |
state.routerViewKey = value; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment