简体   繁体   English

将变量传递给模板回调

[英]Pass variable to template callback

Recently I started getting familiar with meteor platform, and I stumbled on dilemma: Is there a way to pass variables to Template.rendered, Template.created callbacks. 最近,我开始熟悉流星平台,并迷失了困境:是否有一种方法可以将变量传递给Template.rendered,Template.created回调。 Let's say I have route 假设我有路线

Router.route('/profile/:_id', {
    name: 'profile'
});

and I want to somehow to pass that _id variable to Template.rendered callback: 我想以某种方式将该_id变量传递给Template.rendered回调:

Template.profile.rendered = function () {
    //how can I get "_id" in here?
};

Is this possible? 这可能吗? If so how can I do it? 如果可以,我该怎么办?

Usually when you declare a route, you also specify a data context to provide to the template that will be rendered : 通常,当您声明一条路线时,您还要指定一个数据上下文以提供给将要呈现的模板:

Router.route('/profile/:_id', {
  name: 'profile',
  data: function(){
    return Meteor.users.findOne(this.params._id);
  }
});

This way you can reference the user _id coming from the current data context assigned to the profile template like this : 这样,您可以引用来自用户_id的用户_id,该用户_id来自分配给配置文件模板的当前数据上下文,如下所示:

Template.profile.rendered = function () {
  console.log(this.data._id);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM