I'm in the process of creating a basic cache service in Angular; a service that includes a simple setter/getter function for different components to access data from.
Unfortunately, when attempting to subscribe to this service to retrieve the data, the subscribe callback is never triggered and the data remains unretrievable.
catch-service.service.js
import { Injectable } from '@angular/core';
import { Observable, from } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CacheService {
cacheData: Observable<any>;
constructor() { }
setCacheData(data) {
this.cacheData = new Observable(data);
}
getCacheData(): Observable<any> {
return from(this.cacheData);
}
}
component.component.js
...
export class SomeComponent {
constructor(private cacheService: CacheService) { }
data = null;
ngOnInit() {
this.cacheService.getCacheData().subscribe(resp => {
console.log(resp);
this.data = resp;
});
}
...
The service has been injected into the module class without any compilation errors. However, the expected data does not get assigned to the component.