ember.js - ApplicationRouteでは、働いていない
-
21-12-2019 - |
質問
ユーザーがログインしているときはいつでもブール変数を設定しようとしています。
App.ApplicationRoute = Ember.Route.extend({
isLoggedIn: false,
init: function() {
if (loggedIn) {
this.set('isLoggedIn', true);
} else {
console.log(error)
this.set('isLoggedIn', false);
}
});
}
});
.
しかし、this.set()
でGet:
アカウントのTypeError:未定義は関数ではありません
任意のアイデア?
私はユーザーセッションを処理するための最良の場所がすべてのものの根以来、App.ApplicationRoute
にあると考えました。これは問題を引き起こしますか?
更新: フィードバックでは現在/フルコードがあります。
App.ApplicationRoute = Ember.Route.extend({
isLoggedIn: false,
init: function() {
this._super();
auth = new FirebaseSimpleLogin(appRef, function(error, user) {
if (user) {
console.log(user)
this.set('isLoggedIn', true);
} else {
console.log(error)
this.set('isLoggedIn', false);
}
});
}
})
.
だから私は本当にそれが関連性があると思ったのではなく、問題を追跡するために、私はそれを追加しました。
解決
理由は
と関係がある必要がありますthis
.
あなたのFirebaseImpleginに間違って設定されています。コンストラクタの内側を使用している関数は、外部コンテキストの「これ」への参照で渡されなければなりません。これを行う最も簡単な方法は、これに関数のコードを変更することです。
function(error, user) {
if (user) {
console.log(user)
this.set('isLoggedIn', true);
} else {
console.log(error)
this.set('isLoggedIn', false);
}
}.bind(this));
. 他のヒント
最初のものは最初のもの:)
所属していません StackOverflow