I load my data like this in a Service:
employees: Observable<Employee[]>;
private loadEmployees() {
this.employees = this.db.collection('Employees').snapshotChanges().pipe(
map(changes => changes.map(a => {
const employee = a.payload.doc.data() as Employee;
employee.id = a.payload.doc.id;
return employee;
}))
)
}
I want to subscribe to the employees
Oberservable in multiple Components. Like this:
this.businessService.employees.subscribe(
(employees) => {
this.employees = employees;
}
);
But It only works the first time. When I navigate to another component and do the same again it doesn't work. How can I subscribe to an Oberservable multiple times?
Or is the best way to subscribe directly in my service like this:
employees: Employee[];
private loadEmployees() {
this.db.collection('Employees').snapshotChanges().pipe(
map(changes => changes.map(a => {
const employee = a.payload.doc.data() as Employee;
employee.id = a.payload.doc.id;
return employee;
}))
).subscribe(
employees => {
this.employees = employees;
}
);
}
And then access the employee Array in my components. The problem is that I won't get notified in my components when the employees are loaded/changed. So I would need to have a EventEmitter
that I fire once employees changes and and subscribe to in my components.