如何通过铁路路由器传递路由:流星上的路由器?

时间:2015-07-10 19:31:09

标签: node.js meteor session-variables iron-router

在我的post_item.html中,我得到了这一行:

 <a href="{{pathFor 'postPage' _id=this._id}}" class="discuss btn">Discuss</a>

在我的router.js:

  Router.map(function() {
    this.route('postPage',{path:'/posts/:_id',
    beforeRoutingTo: function(id) {
        console.log("hello!");
       Session.set('currentPostId', id); 
    }
  });
  });

在我的post_page.js

Template.postPage.helpers({
     currentPost: function() {
     return Posts.findOne(Session.get('currentPostId'));
     }
});

在我的post_page.html

<template name="postPage">
 {{#with currentPost}}
 {{> postItem}}
 {{/with}}
</template>

我做错了什么? 如何将ID传递给新的“页面”并显示结果?

2 个答案:

答案 0 :(得分:1)

不确定为什么你需要一个会话,但你的路线看起来不对。试试以下内容:

Router.map(function() {
    this.route('postPage',{path:'/posts/:_id',
    onBeforeAction: function() {
        console.log("hello!");
       Session.set('currentPostId', this.params._id); 
    }
  });
});

答案 1 :(得分:1)

我假设您正在使用“探索流星”一书来处理流星教程。我遇到了同样的问题,但是,我能够在不使用会话的情况下解决它。如果您使用iron router,则不再需要任何助手。

的客户机/ router.js:

Router.map(function() {
    this.route('/post/:_id', function () {
      var item = Posts.findOne({_id: this.params._id});
      this.render('postItem', {data: item});
    },
    {
      name: 'post.show'
    });
});

的客户机/帖/ post_item.html

<a href="{{pathFor route='post.show'}}" class="discuss btn">Discuss</a>

如您所见,在router.js中,从网址解析了ID。另外,我们在 &#39; posts.show&#39; 之后命名/post/:id路线,我们可以从引用该名称的视图中获取网址:{ {1}}

相关问题