简体   繁体   English

骨干路由器:摆脱URL中的#

[英]Backbone Router : Get rid of # in the URL

I'm getting backbone router to work 我正在使用骨干路由器工作

App.Router = Backbone.Router.extend({
      routes: {
        "todo": "todo"
      },
      todo: function() {
        alert(1);
      }
});
Backbone.history.start();

This works well when I goto url : domain:port/#/todo 当我转到url: domain:port /#/ todo时,这很有效

I want this to work without # in the URL, I tried putting pushState parameter as mentioned in the documents. 我希望在URL中没有#的情况下工作,我尝试将pushState参数放在文档中提到。

    Backbone.history.start({pushState: true});

This simply redirects # url to the non hashed one 这只是将#url重定向到非哈希值

domain:port/#/todo (redirects to ==>) domain:port/todo domain:port /#/ todo(重定向到==>)domain:port / todo

But when I visit this URL directly 但是当我直接访问此URL时

domain:port/todo 域:端口/待办事项

it doesn't work: "Cannot GET /todo". 它不起作用:“不能GET / todo”。

Is there any way, I can make this URL work without # in it? 有什么办法,我可以让这个URL在没有#的情况下工作吗?

You need to write server side code to deliver the page for the URL. 您需要编写服务器端代码来传递URL的页面。

Using pushState just tells the browser "I have used JS to change the content of the page so it matches what you would get if you ask for this URL". 使用pushState只是告诉浏览器“我已经使用JS来改变页面的内容,所以它与你要求提供这个URL的内容相匹配”。

It doesn't cause the content to match that URL, you have to do that as a separate task. 它不会使内容与该URL匹配,您必须将其作为单独的任务执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM