I'm trying to use Subject and a shared service to transfer data from one component to another. But it isn't working.
Please note that I listed the service in the module level instead of the component decorator. Also perhaps worth noting is that these two components do not share a parent-child relationship.
My service (SearchService):
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SearchService {
public messageSource = new Subject<any>();
constructor() { }
topAlbums(album){
this.messageSource.next(album);
}
}
Component 1 (That is sending the data). When createList() is triggered, it navigates to the new route perfectly but the message that I'm subscribing to below (in Component 2) does not show.
import { SearchService } from '../../services/search.service';
export class AlbumComponent implements OnInit {
private UNIQUE_ID: string;
constructor(private _service: SearchService, private _http: Http, private router: Router) { }
//Linked to a click event handler
createList(){
//API Call to retrieve an ID
this._http.post('https://api.url', data)
.map(res => res.json())
.subscribe(item => {
this.ID = item.id;
let parsed_JSON = JSON.parse(item.files.myAlbums.content);
this.router.navigate(['/list', this.UNIQUE_ID]);
})
this._service.topAlbums("Testing out by sending a dummy message");
}
Component 2 (Receiving the data):
import { SearchService } from '../../services/search.service';
export class ListComponent implements OnInit {
constructor(private _service: SearchService) { }
ngOnInit(){
this._service.messageSource.subscribe(data => console.log(data));
}
}