gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:30:13 25 4
gpt4 key购买 nike

我无法从子助手中获取和设置父模板变量。我已经阅读了很多关于这个问题的页面,但我被卡住了......

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

期望像这样的 child 可以访问:

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

}});

在哪里

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

我错过了什么吗? (Session解决方案这里不是案例...)

最佳答案

有一种更聪明的方法可以完成这项任务。但是您需要明确声明您希望在子模板中访问哪些变量。很可能您希望 var 具有反应性。

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

在Blaze中,像这样调用子模板的时候需要传递这个变量。

{{>child myVar=myVar }}

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

关于javascript - Meteor - 从子助手访问父模板变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35673121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com