How to use services to abstract API endpoints and data models from components

0 votes
Can i know How to use services to abstract API endpoints and data models from components?
5 days ago in Node-js by Nidhi
• 15,620 points
47 views

1 answer to this question.

0 votes

To abstract API endpoints and data models from components in Angular, use services to handle all HTTP calls and data logic.

Steps:

Create a service to manage API calls:

// user.service.ts

@Injectable({ providedIn: 'root' })

export class UserService {

  constructor(private http: HttpClient) {}

  getUsers() {

    return this.http.get<User[]>('/api/users');

  }

  createUser(data: User) {

    return this.http.post('/api/users', data);

  }

}

Use the service in components:

// user.component.ts

this.userService.getUsers().subscribe(users => {

  this.users = users;

});

answered 2 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer

How to return data from Axios API?

Hello @kartik, The issue is that the original axiosTest() function ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,840 points
45,633 views
0 votes
1 answer
0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
1,730 views
0 votes
1 answer

How to Get data from fs.readFile?

Hello @kartik, The function you have defined is ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,840 points
7,539 views
0 votes
1 answer
0 votes
1 answer

How to design a pipe that accepts configuration options for flexible transformations?

Angular Pipe Implementation import { Pipe, PipeTransform } ...READ MORE

answered 2 days ago in Node-js by anonymous
23 views
0 votes
1 answer

How to create a service that manages user sessions and authentication tokens?

1. Create the Auth Service (auth.service.ts) import { ...READ MORE

answered 2 days ago in Node-js by anonymous
29 views
0 votes
1 answer
0 votes
1 answer

How to use middleware for logging actions and state changes in Redux?

To use middleware for logging actions and ...READ MORE

answered Mar 21 in Node-js by Anvi
75 views
0 votes
1 answer

How to use props in a Class-based Component to display data?

In a class-based React component, you can ...READ MORE

answered Mar 24 in Node-js by anonymous
86 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP