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

处理每切换一次页面就增加一条sidebarItems数据的问题 #326

Closed
wants to merge 3 commits into from

Conversation

xiyueW
Copy link

@xiyueW xiyueW commented Jan 3, 2025

问题:
在使用useSidebarItems时,watch监听sidebarItems,发现每切换一次页面,sidebarItems打印的数量就加一
watch(sidebarItems,(n)=>{ console.log(n) })
处理:
可能原因是VPPage组件中调用setupHeaders函数中,此函数每调用一次会给router添加一次beforeEach,导致headersRef改变,从而影响provide的sidebarItems的改变。

Copy link

netlify bot commented Jan 3, 2025

Deploy Preview for vuepress-ecosystem ready!

Name Link
🔨 Latest commit d462e4c
🔍 Latest deploy log https://app.netlify.com/sites/vuepress-ecosystem/deploys/67874ee9aec4fd0008c87d64
😎 Deploy Preview https://deploy-preview-326--vuepress-ecosystem.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@Mister-Hope Mister-Hope requested a review from pengzhanbo January 3, 2025 09:45
@Mister-Hope
Copy link
Member

I don't understand why this fix is valid and why it should be "fixed this way". Detailed explanation would help.

避免重复添加router.beforeEach
@xiyueW
Copy link
Author

xiyueW commented Jan 15, 2025

问题回显:

  1. 新启一个项目,在config.js中添加两个sidebar配置作为测试:
    { sidebar: { '/get-started/': ['started.md', 'started2.md'] } }
  2. 在源码lib/client/layouts/Layout.vue文件监听sidebarItems:
    watch(sidebarItems,(n)=>{ console.log(n) })
    20250115-141502

问题分析:
页面切换VPPage.vue会执行setupHeaders函数,router.beforeEach就会多添加一次执行函数,导致每切换一次路由,router.beforeEach的函数会多执行一个,从而多影响一次sidebarItems。

@Mister-Hope
Copy link
Member

Great thanks for the explanation, I decide to fix in another way.

The other way adds a new alias @theme/useHeaders for people to override the default logic if necessary easily while decoupling from sidebar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants