tutorial-angular/src/components/users/users.component.ts

45 lines
1.0 KiB
TypeScript

import { Component, OnInit } from "@angular/core";
import { ServicesService } from "src/services/services.service";
@Component({
selector: "app-users",
templateUrl: "./users.component.html",
styleUrls: ["./users.component.scss"],
})
export class UsersComponent implements OnInit {
users: any[] = []; // where you store users
newUser: any = {
name: 'test',
email: 'test',
role: ['Admin']
};
constructor(private servicesService: ServicesService) {}
ngOnInit() {
this.getUsers();
}
getUsers() {
this.servicesService.get<any[]>("users").subscribe(
(data) => {
this.users = data;
console.log("Users loaded:", data);
},
(error) => {
console.error("Error loading users:", error);
}
);
}
createUser() {
this.servicesService.post('users', this.newUser)
.subscribe(
response => {
console.log('User created successfully:', response);
},
error => {
console.error('Error creating user:', error);
}
);
}
}