Skip to content

Commit

Permalink
Merge pull request #8 from aaronlamz/dev-0920
Browse files Browse the repository at this point in the history
fix router hash encode
  • Loading branch information
aaronlamz authored Sep 20, 2024
2 parents bcf8dee + 73e0004 commit a1014de
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
You can install `navpress` globally via npm:

```bash
npm install -g navpress@1.0.0-beta.6
npm install -g navpress@latest
```

Or install it locally in your project:

```bash
npm install navpress@1.0.0-beta.6 --save-dev
npm install navpress@latest --save-dev
```

## Quick Start
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "navpress",
"version": "1.0.0-beta.6",
"version": "1.0.0-beta.7",
"description": "Easy navigation website generator",
"main": "index.js",
"type": "module",
Expand Down
20 changes: 11 additions & 9 deletions src/pages/LinksPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ export default {
},
methods: {
scrollToAnchor(hash) {
if (hash) {
nextTick(() => {
const element = document.querySelector(hash)
if (element) {
element.scrollIntoView({ behavior: 'smooth' })
}
})
}
},
if (hash) {
const decodedHash = decodeURIComponent(hash)
nextTick(() => {
const element = document.querySelector(decodedHash)
if (element) {
element.scrollIntoView({ behavior: 'smooth' })
}
})
}
},
},
}
</script>
Expand Down
24 changes: 21 additions & 3 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,27 @@ export function createRouterInstance(config) {
}

const routes = generateRoutes(config.sidebar)

return createRouter({
history: isServer ? createMemoryHistory() : createWebHashHistory(), // 使用 hash 模式
const router = createRouter({
history: isServer ? createMemoryHistory() : createWebHashHistory(),
routes,
})

// 路由全局前置守卫,处理路径中的 %23 锚点编码问题
router.beforeEach((to, from, next) => {
// 检查路径中是否包含编码的 %23
if (to.path.includes('%23')) {
// 解码路径
const decodedPath = decodeURIComponent(to.path)
// 替换掉 path 中的 %23 为正常的 #
if (decodedPath !== to.path) {
next({ ...to, path: decodedPath, replace: true })
} else {
next()
}
} else {
next()
}
})

return router
}

0 comments on commit a1014de

Please sign in to comment.