简体   繁体   English

Meteor - 从子助手访问父模板变量

[英]Meteor - accessing parent template vars from child helpers

I cannot get and set the parent template variable from within the child helper. 我无法从子帮助程序中获取并设置父模板变量。 I have read plenty of pages on the issue but I'm stuck ... 我已经阅读了很多关于这个问题的网页,但是我被困了......

Template.parent.onCreated(function() {  
    this.myVar = new ReactiveVar('xyz');
});  

is expected to be accessible in child like this: 预计这样的孩子可以访问:

Template.child.helpers({
setAndGetIt : function() {
    Template.parentData(1).myVar = 'testString';
    return Template.parentData(1).myVar;

}});

where 哪里

<template name="parent>    
    {{>child}}  
</template>  

Do I miss something? 我错过了什么吗? (Sessions solution is not a case here...) (Sessions解决方案不是这里的例子......)

There's a smarter way to accomplish this task. 有一种更聪明的方法可以完成这项任务。 But you need to explicitly declare which variables you want to be accessible in the child template. 但是您需要明确声明要在子模板中访问哪些变量。 and most probably you want that var to be reactive. 而且很可能你想让var变成反应性的。

Template.parentTemplate.onCreated(function(){
    this.myVar = new ReactiveVar('hello');
});

in Blaze, you need to pass this variable when calling the child template like this. 在Blaze中,你需要在调用这样的子模板时传递这个变量。

{{>child myVar=myVar }}

Template.child.onRendered(function(){
    console.log(this.myVar.get());
});

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

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