Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 为什么主干路由需要默认路由_Backbone.js_Router - Fatal编程技术网

Backbone.js 为什么主干路由需要默认路由

Backbone.js 为什么主干路由需要默认路由,backbone.js,router,Backbone.js,Router,我使用backbone.js@创建了一个测试用例: 路线定义为 var AppRouter = Backbone.Router.extend({ routes: { "/posts/:id" : "getPost", "/download/*path": "downloadFile", "*actions" : "defaultRoute" }, getPost: fun

我使用backbone.js@创建了一个测试用例:

路线定义为

var AppRouter = Backbone.Router.extend({
        routes: {
            "/posts/:id" : "getPost",
            "/download/*path": "downloadFile",  
            "*actions" : "defaultRoute"
        },
        getPost: function(id) {
            alert(id);
        },
        defaultRoute : function(actions){
            alert(actions);
        },
        downloadFile: function( path ){ 
            alert(path); // user/images/hey.gif 
        },
        loadView: function( route, action ){ 
            alert(route + "_" + action); // dashboard_graph 
        }
    });

    var app_router = new AppRouter;

    Backbone.history.start();​
当我改变函数时

  defaultRoute : function(actions){
            alert(actions);
        },

所有其他路由都无法工作,这意味着不会弹出对话框

但当重新更改代码时,一切都正常

这真的很奇怪,让我很困惑。
SOS…

正如您所知,默认路由是唯一可以触发的路由。如果你想让另外两条路线开火,你必须去掉前面的斜线

routes: {
  "posts/:id" : "getPost",
  "download/*path": "downloadFile",  
  "*actions" : "defaultRoute"
}

正如您所知,代码
defaultRoute
是唯一触发的路由。如果你想让另外两条路线开火,你必须去掉前面的斜线

routes: {
  "posts/:id" : "getPost",
  "download/*path": "downloadFile",  
  "*actions" : "defaultRoute"
}