我使用的是ToDo示例中相同的基本路由器代码,但我遇到了一些问题。当我Router.navigateToItem(itemID)一切正常。但是,如果我输入直接url(/ inventory/itemId),那么Session变量永远不会被设置,因此直接的URL不起作用。我可以弄清楚如何让路由器通过直接URL触发。感谢帮助,非常感谢。这里是我正在使用的代码:骨干路由器不会触发直接URL - 使用流星
var WebRouter = Backbone.Router.extend({
routes: {
"": "main",
"inventory/:itemId": "itemDetail"
},
main: function() {
Session.set("inventoryItem", null);
},
itemDetail: function(itemId) {
Session.set("inventoryItem", Items.find({_id:itemId}).fetch()[0]);
},
navigateToItem: function(itemId) {
this.navigate("inventory/"+itemId, {trigger: true});
}
});
Router = new WebRouter;
Meteor.startup(function() {
Backbone.history.start({pushState: true});
});
编辑1:
我注意到,如果我不pushState的:
Backbone.history.start();
然后一切似乎工作。不过,后来我在我的网址这个愚蠢的哈希符号,我不知道它是从哪里来的:/#库存/ WsL7YZxiWk3Cv3CgT
越来越近......我也不清楚什么是不pushState的丢失......
编辑2:
另一个失败的尝试:
window.onload= function(){
var url = window.location.pathname;
Router.navigate(url.substring(1,url.length), {trigger: true});
console.log(url.substring(1,url.length));
};
我真的以为这一次是去上班,但它不...
这并不能解决你目前的问题,但你有没有尝试[流星路由器](https://github.com/tmeasday/meteor-router)?它使用起来非常简单(IMO),并且可以工作到IE8。 –
即时通讯网络开发新手。这对我来说看起来不太简单 - 我不知道page.js是什么,然后我开始浏览并找到越来越多的我不明白的地方......:/ – Chet
是的,你需要陨石来使用它,但是一旦你安装了陨石,只需要编写'mrt add router'来将路由器添加到你的项目中即可。它会在这个过程中添加page.js(并且你不需要知道关于page.js的任何内容,它只是依赖于它)。 –