Working on an angular 6 application. Where I have to fetch and display list of users in every page from API. I have wrote a function which works perfectly:
listUsers() {
let result = this.http.get('xyz.com/api/users');
result.toPromise()
.then(res => {
this.userList = res.json();
})
.catch(err => {
this.msgService.add({severity: 'error', summary: 'Internal Server Error',
detail: 'There is an Server Error occurred. Please try again later.'});
} );
}
My issue is, I want fetch users in more than 10 components/pages. I don't want to repeat(biolerplate) the above function listUsers()
in all those 10 components.
I want to fetch all the users in once central service and access it from every component.
Ho do I do that?
Sorry that I did not phrases the question properly. Also I could not find any similar posts on this. Please point me to the post if it's already answered before. I will delete this duplicate post.