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
Javascript BackboneJS:router.navigate不是一个函数_Javascript_Backbone.js_Browser History - Fatal编程技术网

Javascript BackboneJS:router.navigate不是一个函数

Javascript BackboneJS:router.navigate不是一个函数,javascript,backbone.js,browser-history,Javascript,Backbone.js,Browser History,我正试图用以下内容构建微州历史: var router = Backbone.Router.extend({ routes:{"":"home",":slug":"pages"}, pages:function(slug){alert(slug);}, home:function(){alert('home');} }) ; Backbone.history.start({ pushState: true, silent: true }); $(doc

我正试图用以下内容构建微州历史:

var router = Backbone.Router.extend({
    routes:{"":"home",":slug":"pages"},
    pages:function(slug){alert(slug);},
    home:function(){alert('home');}
}) ;

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

$(document).on("click","a",function(e) {
    e.preventDefault() ;
    router.navigate($(this).attr('href'),true) ;
});
但控制台显示:“未捕获类型错误:router.navigate不是一个函数”


我缺少什么?

您需要实例化您的路由器:

var AppRouter = Backbone.Router.extend({
    routes:{"":"home",":slug":"pages"},
    pages:function(slug){alert(slug);},
    home:function(){alert('home');}
});

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

var router = new AppRouter();
$(document).on("click","a",function(e) {
    e.preventDefault() ;
    router.navigate($(this).attr('href'),true) ;
});

您需要实例化路由器:

var AppRouter = Backbone.Router.extend({
    routes:{"":"home",":slug":"pages"},
    pages:function(slug){alert(slug);},
    home:function(){alert('home');}
});

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

var router = new AppRouter();
$(document).on("click","a",function(e) {
    e.preventDefault() ;
    router.navigate($(this).attr('href'),true) ;
});