【问题标题】:How to access child context in parent templates?如何访问父模板中的子上下文?
【发布时间】:2015-05-27 20:28:50
【问题描述】:

如果子模板在onCreated 中定义了一些东西,

Template.test.onCreated(function() {
  this.xxx = 'test';
});

我想访问父级的子上下文。我该怎么做?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您可以使用 Session 或 ReactiveVar 进行此操作。但是,如果您需要将其保存在子模板上,我认为您可以使用:

    var childView = Blaze.getView(currentTemplate.find('#your-child-theme-id'));
    

    然后通过以下方式获取模板实例:

    var childTemplateInstance = childView._templateInstance;
    

    然后

    var myXXXvar = childTemplateInstance.xxx;
    

    但这有点脏:/我认为没有针对这个特定用例的 Meteor API。我认为你应该使用响应式变量或仅限客户端的集合等。我不知道你想要实现什么。

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 2015-05-24
      • 2015-05-06
      • 1970-01-01
      • 2016-01-09
      • 2011-08-29
      • 2015-01-02
      • 1970-01-01
      • 2016-06-29
      相关资源
      最近更新 更多