这个问题说明了一切。我试图在page.js examples工作中examples/hash/
下的示例以与它相同的方式进行,只使用{ hashbang: true }
选项,但无济于事。
我也试过设置<base href="/hash/">
,但这似乎进入无限重定向。之后,删除page.base('/hash')
似乎可以解决问题,但位置栏会显示http://localhost:4000/hash/#!/hash/
,#
和#subsection
链接仍然无法正常工作。
这是代码:
<!DOCTYPE html>
<html>
<head>
<title>Hash</title>
<style>
body p {
color: #333;
font-family: verdana;
}
#sections p {
height: 500px;
margin: 30px;
padding: 30px;
line-height: 40px;
background-color: #eee;
border: 1px solid #ccb;
font-size: 30px;
}
#sections p a {
display: block;
float: right;
font-size: 14px;
}
</style>
<script src="/page.js"></script>
</head>
<body>
<h1 id="top">Hash</h1>
<ul>
<li><a href="#">#</a></li>
<li><a href="#subsection">#subsection</a></li>
<li><a href="section?name=tana">section?name=tana</a></li>
<li><a href="section?name=tana#subsection">section?name=tana#subsection</a></li>
</ul>
<div id="sections">
<p><strong>A</strong><a href="#top">top</a></p>
<p><strong>B</strong><a href="#top">top</a></p>
<p id="subsection"><strong>C</strong><a href="#top">top</a></p>
</div>
<script>
page.base('/hash');
page('/:section', section);
page();
function section(ctx, next) {
console.log('path: ', ctx.path);
console.log('querystring: ', ctx.querystring);
console.log('hash: ', ctx.hash);
console.log(' ');
}
</script>
</body>
</html>
我该怎么做? 感谢
答案 0 :(得分:0)
我遇到了同样的问题,无法找到解决方案。我最终做的是使用hacky解决方法。
如果您不必支持旧浏览器,则可以使用history.pushState
推送网址的正确版本。
history.pushState('','',location.pathname + location.search);
这在美容上解决了这个问题,但是深入链接到复杂路径需要更多的逻辑。