我正试图用这个来构建微观历史:
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) ;
});
但是控制台说:“未捕获TypeError:router.navigate不是函数”
我在那里错过了什么?
答案 0 :(得分:4)
您需要实例化路由器:
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) ;
});