简体   繁体   English

在Meteor中的onRendered中获取模板变量

[英]Get template variable inside onRendered in Meteor

I have a template settings in which I have an autoform. 我有一个模板settings ,其中有一个自动表单。

I add a hook to my autoform inside the onRendered function 我在onRendered函数中的自动表单中添加了一个挂钩

Template.settings.onRendered( () => {
  AutoForm.addHooks( 'editForm', {
    onSuccess: function( formType, result ) {
      //
    }
  } );
} );

How can I, inside my onSuccess get a variable populated in the template through Template.settings.helpers ? 我如何在onSuccess内部通过Template.settings.helpers在模板中填充变量?

I have tried Template.instance().variableName , but it cannot be found. 我已经尝试过Template.instance().variableName ,但是找不到。

From the Meteor docs: 从Meteor文档中:

In the body of a callback, this is a template instance object that is unique to this occurrence of the template and persists across re-renderings. 在回调的主体中,这是一个模板实例对象,对于模板的这种出现是唯一的,并且在重新渲染期间保持不变。 Use the onCreated and onDestroyed callbacks to perform initialization or clean-up on the object. 使用onCreated和onDestroyed回调对对象执行初始化或清理。

http://docs.meteor.com/#/full/template_onRendered http://docs.meteor.com/#/full/template_onRendered

But I think you need to make use of function instead of an ES2015 arrow function or this won't be as expected. 但我认为你需要使用的function ,而不是一个ES2015箭头功能或this将无法达到预期。

Also, you would need to add the line: const self = this; 另外,您需要添加以下行: const self = this; And then use self if you want to access it within the Autoform function. 然后,如果要在Autoform函数中访问self ,请使用self

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

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