1
我有兩個組件,我需要在兩個組件中顯示相同的數據。 我的服務:承諾服務共享數據
export default class Logs {
constructor($http, $q, httpService) {
this.$http = $http;
this.$q = $q;
this.$rootScope = $rootScope;
this.httpService = httpService;
this.logs = null;
this.getDefer = this.$q.defer();
this.loadData();
}
loadData() {
var request = {url: '/logs'};
this.httpService.getJson(request)
.then((response) => {
this.logs = response.data;
this.getDefer.resolve(this.logs);
})
.catch((err) => {
this.getDefer.reject(err);
});
return this.getDefer.promise;
}
getLogs() {
return this.getDefer.promise;
}
create(newLog) {
return this.$http.post('http://localhost:9000/api/logs', newLog);
}
}
// this is example of using service in component
this.logService.getLogs().then((result) => {
this.logs = result;
});
我使用的承諾,因爲,getLogs
在幾個組件使用,並幫助我避免在應用程序啓動雙重要求。但現在,我想知道,如何在更新或創建後如何調用getLogs
來獲取新數據?
謝謝,這是我需要的 – user348173
不客氣:) – Kindzoku