铁路由器:如何使用this.params._id设置会话

mar*_*cks 4 meteor iron-router

我有一个路线/记分牌/ 1,其中1不是一个对象的宁静id,而是一个法院ID,我想用它来选择法院的游戏.

我试过这个代码来设置会话但不加载模板.如果我删除事件和硬编码var court,模板会正常加载.

this.route('scoreboard',
    {

    path: '/scoreboard/:_id',

    template: 'scoreboard',

    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }

  }); //route
Run Code Online (Sandbox Code Playgroud)

我发现这似乎有效.什么不起作用是这样的:

var court = Session.get("court");

console.log(court); -> 1

myGame = Games.findOne({court_id: court});
Run Code Online (Sandbox Code Playgroud)

虽然这有效:

myGame = Games.findOne({court_id: 1});
Run Code Online (Sandbox Code Playgroud)

找到了!

  var court = parseInt(Session.get('court'));
Run Code Online (Sandbox Code Playgroud)

Chr*_*itz 8

这对我有用:

$ meteor create test
$ cd test
$ meteor add iron:router
Run Code Online (Sandbox Code Playgroud)

test.js:

if (Meteor.isClient) {
    Template.scoreboard.id = function() {
        return Session.get('court');
    }
}

Router.route('/scoreboard/:_id', {
    name: 'scoreboard',
    path: '/scoreboard/:_id',
    template: 'scoreboard',
    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }
});
Run Code Online (Sandbox Code Playgroud)

的test.html:

<head>
</head>

<template name="scoreboard">
  Scoreboard<br/>
  id is: {{id}}
</template>
Run Code Online (Sandbox Code Playgroud)

然后去localhost:3000/scoreboard/123.