在login之后,铁路路由器中的未定义Meteor.userId()

当用户第一次login时加载站点,然后login,下面定义的路由account将parsing为/profile/null 。 用户必须在path正确之前刷新站点。

 this.route('account', { template: 'profile', path: '/profile/' + Meteor.userId() }) 

创build一个路由,特别是采取参数Meteor.userId()的原因是因为我使用的包 ,需要我定义的path名,即: {{> ionTab title="Account" path="account"}}我不认为可以采取一个参数。

有什么更好的方法来完善这条路线?

当你的应用程序启动时,路由定义会发生一次,此时Meteor.userId()仍然是未定义的,所以这就是为什么你的代码不工作,但是整个方法是错误的,你应该定义你的路由如下:

 Router.route("/profile/:_id",{ name:"profile", template:"profile", waitOn:function(){ return Meteor.subscribe("userProfile",this.params._id); }, data:function(){ var user=Meteor.users.findOne(this.params._id); return { user:user }; } }); 

你可能错过了iron:router文档是可以定义路由采取参数( /path/:param语法),并使用此参数configuration路由订阅和数据上下文。

编辑

如果要获取此路线的相应dynamicpath,可以使用path方法:

HTML

 <template name="myTemplate"> {{> ionTab title="Account" path=accountPath}} </template> 

JS

 Template.myTemplate.helpers({ accountPath:function(){ return Router.path("profile",Meteor.user()); } });