如何在 Meteor Iron 路由器中使用 ID?

How to make use of an ID in meteor iron router?

我知道我可以将 :id 放在我的路线路径中,以便获得特定的 URL。这就是我可以从网上找到的教程中得到的。但是我怎样才能使用这个功能呢?有什么好处? 我假设 ID 像 url 参数一样在内部传递,因为在数据函数中似乎是特定的 return,基于 ID。但我不确定。

this.route('projectView',{
    path:'/projects/:id',
    layoutTemplate:'mainLayout',
    loginRequired:'entrySignIn',
    waitOn:function(){
        Meteor.subscribe('customers');
        return Meteor.subscribe('projects');
    },
    data:function(){
        Session.set('active_project',this.params.id);
        return Projects.findOne({_id:this.params.id});
    },

在您的示例中,path 看起来像 /projects/:id。在引擎盖下,路由器将 :id 的内容转换为 this.params.id,这就是您在 data 挂钩中使用的内容。

换句话说,如果路由器遇到路径/projects/abc123,它就会知道它应该使用projectView路由并且this.params.id应该等于abc123加载相应数据时。