刷新 Angular2 Meteor 后未定义 Meteor.user()
Undefined Meteor.user() after refresh Angular2 Meteor
我对 Angular2 和 Meteor 还很陌生。
我想做一件非常简单的事情:使用 Meteor.user() 从 Meteor 获取当前用户信息。
问题是,每当我刷新页面时,Meteor.user() 的配置文件值变为 "undefined".
我做了一些搜索,显然 Meteor.user() 太长而无法执行。
显然,您可以使用 Tracker.autorun() 等待 Meteor.user() 初始化。
目前我的代码如下所示:
Tracker.autorun(function () {
Meteor.subscribe("userLoggedIn");
console.log(Meteor.user());
if(Meteor.user() !== undefined) {
//Do something
this.myFunction();
}
});
问题是 "myFunction" 是 "undefined" 因为它没有在 Tracker 中定义(我认为至少)。
你知道如何调用我的函数吗?
可以在跟踪器中执行那么多代码吗?
是否可以在 Tracker 之外等待 Meteor.user() 的初始化?
(是的,我有很多问题 :p )
提前谢谢你! :)
如果您依赖 this
,为什么不使用箭头函数来保持上下文?
Tracker.autorun(() => { this.myFunction(); });
你也可以设置一个作用域变量,老派的方式:
var self = this;
Tracker.autorun(function () { /* bla bla */ self.myFunction(); });
我对 Angular2 和 Meteor 还很陌生。 我想做一件非常简单的事情:使用 Meteor.user() 从 Meteor 获取当前用户信息。 问题是,每当我刷新页面时,Meteor.user() 的配置文件值变为 "undefined".
我做了一些搜索,显然 Meteor.user() 太长而无法执行。 显然,您可以使用 Tracker.autorun() 等待 Meteor.user() 初始化。
目前我的代码如下所示:
Tracker.autorun(function () {
Meteor.subscribe("userLoggedIn");
console.log(Meteor.user());
if(Meteor.user() !== undefined) {
//Do something
this.myFunction();
}
});
问题是 "myFunction" 是 "undefined" 因为它没有在 Tracker 中定义(我认为至少)。
你知道如何调用我的函数吗? 可以在跟踪器中执行那么多代码吗? 是否可以在 Tracker 之外等待 Meteor.user() 的初始化?
(是的,我有很多问题 :p )
提前谢谢你! :)
如果您依赖 this
,为什么不使用箭头函数来保持上下文?
Tracker.autorun(() => { this.myFunction(); });
你也可以设置一个作用域变量,老派的方式:
var self = this;
Tracker.autorun(function () { /* bla bla */ self.myFunction(); });