Javascript 在Backbone.js中获取URL片段而不是哈希

Javascript 在Backbone.js中获取URL片段而不是哈希,javascript,backbone.js,url-routing,router,Javascript,Backbone.js,Url Routing,Router,我为此奋斗了很长时间。我正在使用Backbone.js和Require.js。我试图告诉主干网根据当前URL(而不是散列)显示特定视图。我的代码如下所示: define(['jquery','underscore','backbone', 'views/manage_pages'], function($, _, Backbone, ManagePages) { var AppRouter = Backbone.Router.extend({ routes:{ '/ne

我为此奋斗了很长时间。我正在使用Backbone.js和Require.js。我试图告诉主干网根据当前URL(而不是散列)显示特定视图。我的代码如下所示:

define(['jquery','underscore','backbone', 'views/manage_pages'], function($, _, Backbone,  ManagePages) {
  var AppRouter = Backbone.Router.extend({
    routes:{
      '/new_page': "showPageForm",
      '/edit_page': "showPageEditForm",
      '*actions': "showPageForm"
    },
  });
  var initialize = function(){
    appRouter = new AppRouter;
    appRouter.on("route:showPageForm", function(){
      console.log('hej');
    });
    appRouter.on("route:showPageEditForm", function(){
      console.log('ho');
    });
    var page_form = new ManagePages();
      Backbone.history.start();
    }
    return {
      initialize: initialize
    }
  });

因此,基本上,当用户访问它时,应该记录
hej
,当用户访问它时,应该记录
。我不知道如何做到这一点,甚至不知道这是否可能。有人能帮我吗?

您可以使用主干网对的支持,它默认使用URL路径(而不是哈希片段)。调用时只需将其指定为一个选项:

(请注意,如果您的应用程序位于域下的子路径中,那么您可以告诉主干将其用作根--
Backbone.history.start({pushState:true,root://myapprot/“})
,尽管在您的情况下这似乎不是一个问题。)

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