1
我試圖通過轉換現有的應用程序來解決ES6和Angular問題。不出所料,挑戰是this
!ES6:這是什麼等於_this/self
let _$localStorage
來自(Injected dependencies not accessible in class methods for AngularJS),但有一個建議,那裏不是一個好方法。
我試圖實現的下一件事情有同樣的問題,即如何在一個類中有全局變量。
歡迎諮詢
let _$localStorage;
//^^^^^^^^^^^^^^^^^
class MainSvc {
constructor($http, $localStorage) {
this.$http = $http;
_$localStorage = $localStorage;
this.data = {};
//^^^^^^^^
this.production = (location.host === "afbackend.herokuapp.com");
this.baseUrl = location.protocol + '//' + location.host;
console.log('MainSvc');
}
login(data, success, error) {
this.$http.post(this.baseUrl + '/auth', data)
.success(function(res) {
console.log("Login succeeded, token :", res.token);
_$localStorage.token = res.token;
this.data.user = getUserFromToken(res.token);
//^^^^^^^^^^^^^^
console.log(m);
// success();
})
// .error(error);
}
}
OK,這是有道理的,只是我必須聲明自己與回調的任何方法... –
其實受益即使在我的簡單例子中,厚厚的空氣功能也變得非常清晰。沒有'自我'真的需要 –