javascript 单页应用路由器没有散列和小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26395992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:54:34  来源:igfitidea点击:

Single Page Application router without hash and small

javascriptroutersingle-page-application

提问by Florian Mertens

Ok, so I've been googling about this matter for a while now, but I just can't seem to find what I need for a good single-page-application router. What I want:

好的,所以我一直在谷歌上搜索这个问题一段时间,但我似乎无法找到一个好的单页面应用程序路由器所需的东西。我想要的是:

  1. A jscript router library
  2. A router that does NOT do hashtags/hashbangs
  3. With example.com being my site, that handles something like:
  4. If javascript is disabled, the server returns a valid static html page (helps with search indexing too), with similar internal links, that the server can handle itself <- this part I can do myself
  5. If javascript is enabled, the server returns the same static page, but the router script now prevents links from being clicked, and processes the request itself using eg an anonymous function (as is the case with most router libs), in where I could fill the DOM with dynamic JSON data via javascript.
  6. It handles history correctly.
  7. Is small (I don't want to load an extensive framework when I have my own previously chosen framework already)
  1. 一个 jscript 路由器库
  2. 一个不做主题标签/hashbangs的路由器
  3. 以 example.com 作为我的网站,它处理如下内容:
  4. 如果 javascript 被禁用,服务器将返回一个有效的静态 html 页面(也有助于搜索索引),具有类似的内部链接,服务器可以自行处理 <- 这部分我可以自己做
  5. 如果启用了 javascript,服务器将返回相同的静态页面,但路由器脚本现在会阻止链接被点击,并使用匿名函数处理请求本身(大多数路由器库就是这种情况),我可以在其中填写通过 javascript 使用动态 JSON 数据的 DOM。
  6. 它正确处理历史。
  7. 很小(当我已经拥有自己之前选择的框架时,我不想加载一个广泛的框架)

Is there a library out there that exists like that? I looked at crossroads.js, which seems great, but it does do hashbangs/hashtags/hashes. I looked at Backbone.js, but with its numerous features and 6.5Kb size, not too keen. This guy had it pretty much on the spot, but his library didn't seem to be well supported. I was pretty amazed at this quick attempt, but it didn't seem to include cross-browser well-enough, or well supported. There are hundreds more out there, but I have no idea which one I should pick? Did anyone else come accross this rather trivial feature for single-page-applications?

有没有这样的图书馆?我查看了crossroads.js,它看起来很棒,但它确实可以做 hashbangs/hashtags/hashes。我查看了Backbone.js,但它具有众多功能和 6.5Kb 大小,不太热衷。这家伙当场就有了,但他的库似乎没有得到很好的支持。我对这种快速尝试感到非常惊讶,但它似乎并没有足够好地包含跨浏览器,或者没有得到很好的支持。还有数百个,但我不知道我应该选择哪一个?有没有其他人遇到过这个用于单页应用程序的微不足道的功能?

采纳答案by Florian Mertens

I found Page.

我找到了Page

Page is a small client-side routing library for use with building single page applications (SPAs). It has a simple API which is inspired by Express. It utilizes the HTML5 history API under the hood, which is what allows you to build smooth user interfaces while still having linkable URLs for different pages of the app.

Page 是一个小型客户端路由库,用于构建单页应用程序 (SPA)。它有一个受 Express 启发的简单 API。它在底层利用了 HTML5 历史 API,这使您可以构建流畅的用户界面,同时仍然为应用程序的不同页面提供可链接的 URL。

Does also do page state caching, which after a good implementation on my end, works really neatly.

也做页面状态缓存,在我的一个很好的实现之后,它工作得非常好。

回答by vorillaz

Here is a simple tutorialworking along with History API. Since you may want to avoid hashes on your URLs keep in mind that some browsers tend to reload the page if the URL changes.

这是一个使用 History API的简单教程。由于您可能希望避免 URL 上的哈希值,请记住,如果 URL 更改,某些浏览器往往会重新加载页面。