本文介绍了Template.instance() 和 this 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Template.instance() 和这个有什么区别?使用其中一种是否有优势?

Template.name.onRendered( function() {var 模板 = Template.instance();var 实例 = 这个;});
解决方案

当在生命周期事件中时,例如 onCreatedonRenderedonDestroyed, Template.instance() 将返回与 this 相同的对象,因为在这些回调注册函数中,this 被显式设置为当前模板实例由流星.

HTML

<div class="组件"><p>你好 <em>{{name}}</em>,点击按钮!</p><button type="button">点击我!</button><p>您点击了<strong>{{count}}</strong>按钮上的时间.</p>

<身体>{{>组件名称="世界"}}

JS

Template.component.onCreated(function(){console.log(this == Template.instance());//使用它来访问模板实例this.count = new ReactiveVar(0);//使用 this.data 访问数据上下文console.log(this.data.name == "World");});

但是在模板助手中,this 绑定到模板的数据上下文,而不是模板实例本身,因此需要使用 Template.instance() 来访问执行帮助程序的模板实例.

Template.component.helpers({计数:函数(){//使用它来访问数据上下文console.log(this.name);//使用 Template.instance() 访问模板实例返回 Template.instance().count.get();}});

最后,在模板事件中,this 也绑定到模板的数据上下文,但事件处理程序使用额外的 template 参数调用,您可以使用该参数作为 Template.instance() 的简写.

Template.component.events({点击按钮":功能(事件,模板){//使用它来访问数据上下文console.log(this.name);//使用参数访问模板实例var currentCount = template.count.get();template.count.set(currentCount + 1);}});

What is the difference between Template.instance() and this? Is there an advantage to use one or the other?

Template.name.onRendered( function() {
 var template = Template.instance(); 
 var instance = this;
});
解决方案

When inside a lifecycle event such as onCreated, onRendered or onDestroyed, Template.instance() will return the same object as this because in these callback registration functions, this is explicitly set to the current template instance by Meteor.

HTML

<template name="component">
  <div class="component">
    <p>Hello <em>{{name}}</em>, click the button!</p>
    <button type="button">Click me!</button>
    <p>You clicked <strong>{{count}}</strong> time(s) on the button.</p>
  </div>
</template>

<body>
  {{> component name="World"}}
</body>

JS

Template.component.onCreated(function(){
  console.log(this == Template.instance());
  // use this to access the template instance
  this.count = new ReactiveVar(0);
  // use this.data to access the data context
  console.log(this.data.name == "World");
});

However in template helpers, this is bound to the data context of the template, not the template instance itself, so using Template.instance() is necessary to access the template instance executing the helper.

Template.component.helpers({
  count: function(){
    // use this to access the data context
    console.log(this.name);
    // use Template.instance() to access the template instance
    return Template.instance().count.get();
  }
});

Finally, in template events, this is also bound to the data context of the template, but the event handler is called with an additional template parameter that you may use as a shorthand to Template.instance().

Template.component.events({
  "click button": function(event, template){
    // use this to access the data context
    console.log(this.name);
    // use the argument to access the template instance
    var currentCount = template.count.get();
    template.count.set(currentCount + 1);
  }
});

这篇关于Template.instance() 和 this 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 08:15