我想获得一些帮助来调试已定义的 react 变量未定义的情况。
此代码将 react 变量附加到模板实例,并在 template.autorun()
中使用该变量。
Template.home.onCreated(function () {
this.limit = new ReactiveVar(15);
this.autorun(function () {
this.subscribe('recent-topics', this.limit.get());
});
});
当我第一次加载模板时,我希望模板使用参数15
订阅recent-topics
。但是,代码会抛出错误:
Uncaught TypeError: Cannot read property 'get' of undefined
有什么想法吗?
最佳答案
只是为了传播 ES6 的乐趣而给出的答案:
Template.home.onCreated(function () {
this.limit = new ReactiveVar(15);
this.autorun(() => {
this.subscribe('recent-topics', this.limit.get());
});
});
确保添加 grigio:babel
包,并且您的 Javascript 文件以 .es6.js
、.es6
或 .jsx
.
说明
在 ES6(又名 ECMAScript 6)中,有一个新的“粗箭头”语法,与 CoffeeScript 的实现非常相似。在 ES6 中,当你做这样的事情时:
someFunc = function () {
anotherThing((var1, var2) => {
this.thing = true;
});
};
与执行此操作相同:
someFunc = function () {
var self = this;
anotherThing(function (var1, var2) {
self.thing = true;
});
};
关于javascript - react 变量定义时未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31576869/