3

我最近用 iron router for meteor,我惊讶地发现使用路由器从一个页面浏览到另一个页面实际上会重新加载整个页面内容。这是由于从一个页面浏览到另一个页面时要更改的 url 路径。它可能对搜索引擎可浏览的网站有用,但构建 Web 应用程序这不是我想要的。

实际上,我期待一些基于片段的东西,例如更改片段会将其放入会话中,以便我可以直接在我的模板中使用它。下面的代码应该产生想要的效果(jQuery):

$(window).on('hashchange', function() {
  Session.set('hash', window.location.hash);
});

但在使用我自己的解决方案之前,我想确保我不会重新发明轮子。是否有任何流星路由器或任何其他框架支持基于片段的页面渲染(与路径相反)?

4

2 回答 2

1

只要我们通过标签<a href="/somePage">ClickMe</a>或通过路由器功能执行此操作,iron-router 实际上不会重新加载整个页面Router.go('/somePage'); 但是在浏览器的字段中手动引入 url 会重新加载整个页面(使用片段不会是这种情况)

对于那些希望将片段动态放入会话的人来说,这里是我使用的完整代码:

function storeHashInSession()
{
  Session.set('hash', window.location.hash);
}

Meteor.startup(function()
{
    storeHashInSession();
});

$(window).on('hashchange', function()
{
    storeHashInSession();
});
于 2014-01-20T08:21:31.523 回答
0

I think it depends on what browser you are using. I haven't looked at iron-router in a while but as of version 0.6.0 non-IE browsers used HTML5 history push state while IE<10 made fresh page requests from the server each time.

See this issue for details: https://github.com/EventedMind/iron-router/issues/14

For our startup we are still using mini-pages.

If iron-router doesn't already support push state in IE<10, hopefully it will soon.

于 2014-01-20T01:23:26.950 回答