【问题标题】:MeteorJs getting querystring data for templatehelper useMeteorJs 获取查询字符串数据以供模板助手使用
【发布时间】:2015-01-21 06:49:10
【问题描述】:

我是 Meteor 的新手,对 Meteor 有一点小问题。 我创建了一个链接 http://localhost:3000/game?id=7lJ8F 我将如何获取查询字符串中的 id 值并将其返回给助手。 我一直在寻找答案,但没有找到。

Router.route('/game/:_id', function(){
    Session.set("gameid",this.params.query.id);

  });
  Template.gamebefore.helpers({
    ids: function () {
      return Session.get("gameid");
    }
  });

我知道这都是错误的,但我非常渴望找到答案,所以任何人都会有所帮助。谢谢你!

【问题讨论】:

  • 试试Session.set("gameid",this.params._id);

标签: meteor query-string


【解决方案1】:

如何处理和获取url的参数在guide of iron router中有描述

你可能会错过_:

this.params.query.id 

应该是

this.params.query._id

反之亦然,因为在您的网址中是

/game?id=7lJ8F 

在你的路线中

Router.route('/game/:_id', function(){

【讨论】:

    【解决方案2】:

    路由中不需要 id:

    Router.route('/game', function(){
        Session.set("gameid",this.params.query.id);
    
      });
      Template.gamebefore.helpers({
        ids: function () {
          return Session.get("gameid");
        }
      });
    

    请注意,您可能希望通过使用 http://.../game/somegameid 来使用更多 REST 方法,而不是使用 http://..../game?id=somegameid,在这种情况下,您需要这条路线:

    Router.route('/game/:_id', function(){
        Session.set("gameid",this.params._id);
    
      });
      Template.gamebefore.helpers({
        ids: function () {
          return Session.get("gameid");
        }
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2015-08-15
      • 2015-08-23
      • 2018-05-24
      • 2015-08-11
      • 1970-01-01
      相关资源
      最近更新 更多