我用的是流星1.4。
Template.showProducts.onCreated(() => {
var handle = Meteor.subscribe("products");
//not working: var handle = this.subscribe("products");
//not working: var handle = Template.instance().subscribe("products");
Tracker.autorun(() => {
//not working: this.autorun
const isReady = Meteor.ready();
//not working: this.subscriptionsReady()
if(isReady){
const products = Products.find().fetch();
Session.set("prods", products);
}
});
});如果我用"this.subscribe",我得到:
Uncaught:_this.subscribe不是函数
如果我使用"Template.instance()",我得到:
无法读取属性'subscriptionsReady‘的null
发布于 2016-12-15 07:58:07
如果使用箭头函数,则将丢失Meteor试图传入的this值。相反,使用常规匿名函数(function () { ... })。
然后,您应该使用this.autorun而不是Tracker.autorun。这将确保在模板消失时清除自动运行,并允许Template.instance在自动运行中工作。
发布于 2016-12-15 07:58:38
问题是,您正在向onCreated处理程序传递箭头函数,它不允许绑定this (参考文献)。因此,Meteor无法正确绑定它刚刚创建的模板实例,您的订阅(以及其他各种事情)将失败。
修复方法就是传递onCreated一个传统的JS函数:
Template.showProducts.onCreated(function () {
...https://stackoverflow.com/questions/41158981
复制相似问题