![](/img/trans.png)
[英]Whenever I refresh the page in meteor angular then `Meteor.user()` method returns undefined
[英]Undefined Meteor.user() after refresh Angular2 Meteor
我對Angular2和Meteor還是很陌生。 我想做一個非常簡單的事情:使用Meteor.user()從Meteor獲取當前用戶信息。 關鍵是,每當刷新頁面時,Meteor.user()的配置文件值就會變為“未定義”。
我做了一些搜索,顯然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”是“未定義的”,因為它沒有在Tracker中定義(至少我認為)。
您知道如何調用函數嗎? 在跟蹤器中執行那么多代碼可以嗎? 是否可以在Tracker之外等待Meteor.user()的初始化?
(是的,我有很多問題:p)
預先感謝您! :)
如果您依賴this
,為什么不使用箭頭功能保留上下文?
Tracker.autorun(() => { this.myFunction(); });
您還可以設置范圍變量,舊的方式:
var self = this;
Tracker.autorun(function () { /* bla bla */ self.myFunction(); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.