Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add onBeforePageLoad hook for router #2564

Merged
merged 7 commits into from Jul 2, 2023

Conversation

buqiyuan
Copy link
Contributor

@buqiyuan buqiyuan commented Jul 1, 2023

I am using fullpage.js, but it doesn't work properly in vitepress because it uses location.href for anchors internally, which causes the page to be reloaded in vitepress. Therefore, I want to add an onBeforeRoutePageLoad hook to control the timing of page reloading.

20230701-183916.mp4

docs/components/ComponentInHeader.vue:

<template>
  <span @click="handleClick">&#x26A1;</span>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import { useRouter } from 'vitepress'

const router = useRouter()

onMounted(() => {
  console.log('ComponentInHeader onMounted')
})

router.onBeforeRouteChange = ((to) => {
  console.log('onBeforeRouteChange',to) 
})

const handleClick = () => {
  location.href = '#escaping' 
  // const section = document.querySelector('#escaping')!;
  // section.scrollIntoView({ behavior: 'smooth' }); // 平滑滚动到元素位置
}
</script>

@brc-dd
Copy link
Member

brc-dd commented Jul 1, 2023

why the early return thing?

@buqiyuan
Copy link
Contributor Author

buqiyuan commented Jul 1, 2023

I don't quite understand what you mean, if I return Promise.reject(), vitepress will treat it as an error catch, so I hope that the user's active cancellation can be distinguished from the program error.
My ideal API would be a routing guard of type vue-router, do you have any thoughts on this?
image

@brc-dd brc-dd changed the title feat(client): add onBeforeRoutePageLoad hook for router feat(client): add onBeforePageLoad hook for router Jul 2, 2023
@brc-dd brc-dd merged commit 665f3b0 into vuejs:main Jul 2, 2023
8 checks passed
@buqiyuan
Copy link
Contributor Author

buqiyuan commented Jul 2, 2023

Hello! Thank you very much for your work! However, the issue I encountered has not been resolved yet. When using location.href('#xxx') for anchor links, it triggers the popstate event, but it does not execute the go function, so it does not pass through the onBeforePageLoad hook.

window.addEventListener('popstate', (e) => {
loadPage(location.href, (e.state && e.state.scrollPosition) || 0)
})

@brc-dd
Copy link
Member

brc-dd commented Jul 2, 2023

ah, moving that condition inside loadPage should work then.

@buqiyuan buqiyuan deleted the feat/router_onBeforeRoutePageLoad branch July 2, 2023 09:01
@brc-dd
Copy link
Member

brc-dd commented Jul 2, 2023

fixed in a3df617

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants