I'm new to angular and i try to build a small app. I have this service who holds a variable and a function:
data.service.ts
data = [];
getData() {
this.http.get(url).subscribe((res)=>
this.data.push(res));
};
I also have two routes:
app.module.ts (the service is defined here)
import { DataService } from './data.service';
// ...
const appRoutes = [
{ path: 'home', component: HomeComponent },
{ path: 'settings', component: SettingsComponent }
]
@NgModule({
// ..
providers: [DataService],
// ..
})
When i use the getData function in the homeComponent it works fine, and the response is added to the data array. But- and here is the problem- once I navigate to the second route, the data in the service is back to be an empty array.
I would have think that the service is not being "restarted" when i navigate.
So, what is the problem? Thank you.