骨干路由器:去掉 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();
当我转到 url 时效果很好:domain:port/#/todo
This works well when I goto url : domain:port/#/todo
我希望在 URL 中没有 # 的情况下工作,我尝试将 pushState 参数放在文档中.
I want this to work without # in the URL, I tried putting pushState parameter as mentioned in the documents.
Backbone.history.start({pushState: true});
这只是将 # url 重定向到非散列的
This simply redirects # url to the non hashed one
domain:port/#/todo(重定向到 ==>) domain:port/todo
但是当我直接访问这个网址时
But when I visit this URL directly
域:端口/待办事项
它不起作用:无法 GET/todo".
it doesn't work: "Cannot GET /todo".
有什么办法可以让这个 URL 在没有 # 的情况下工作吗?
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.
使用 pushState
只是告诉浏览器我使用 JS 更改了页面的内容,因此它与您请求此 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".
它不会导致内容与该 URL 匹配,您必须将其作为单独的任务来执行.
It doesn't cause the content to match that URL, you have to do that as a separate task.
这篇关于骨干路由器:去掉 URL 中的 #的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!