oktatas:web:angular:angular_httpclient:dolgozok
Különbségek
A kiválasztott változat és az aktuális verzió közötti különbségek a következők.
Előző változat mindkét oldalonElőző változatKövetkező változat | Előző változat | ||
oktatas:web:angular:angular_httpclient:dolgozok [2023/02/21 11:50] – [Angular HttpClient - Dolgozók] admin | oktatas:web:angular:angular_httpclient:dolgozok [2025/03/02 13:40] (aktuális) – eltávolítva admin | ||
---|---|---|---|
Sor 1: | Sor 1: | ||
- | [[oktatas: | ||
- | |||
- | ====== Angular HttpClient - Dolgozók ====== | ||
- | |||
- | * **Szerző: | ||
- | * Copyright (c) Sallai András, 2022, 2023 | ||
- | * Licenc: [[https:// | ||
- | * Web: https:// | ||
- | |||
- | ===== Bevezetés ===== | ||
- | |||
- | Ebben a leírásban dolgozók adatait fogjuk kezelni, a teljes **CRUD** műveleteket | ||
- | megvalósítva, | ||
- | |||
- | |||
- | ===== Backend oldal ===== | ||
- | |||
- | Szükségünk van egy REST API szerverre, a következő végpontokkal. | ||
- | Ennek elkészítését lásd [[oktatas: | ||
- | |||
- | ^ Végpont | ||
- | | / | ||
- | | / | ||
- | | / | ||
- | | / | ||
- | | / | ||
- | | / | ||
- | | / | ||
- | | / | ||
- | |||
- | ==== GitHub-on ==== | ||
- | |||
- | A backend elérhető a GitHubon a következő címen: | ||
- | |||
- | * https:// | ||
- | |||
- | Beüzemelés a projekt docs könyvtárában. | ||
- | |||
- | ===== REST API kliens ===== | ||
- | ==== Projekt készítése ==== | ||
- | |||
- | Készítsünk egy angular projektet: | ||
- | ng new app01 | ||
- | |||
- | Kérjünk router szolgáltatást. | ||
- | |||
- | Lépjünk be, majd indítsunk kódszerkesztőt. | ||
- | |||
- | cd app01 | ||
- | code . | ||
- | |||
- | |||
- | ===== Azonosítás ===== | ||
- | |||
- | |||
- | ==== Szükséges modul ==== | ||
- | |||
- | <code javascript src/ | ||
- | import { HttpClientModule } from ' | ||
- | //... | ||
- | |||
- | imports: [ | ||
- | HttpClientModule | ||
- | ], | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ==== Szolgáltatás létrehozása | ||
- | |||
- | Készítsünk egy szolgáltatást, | ||
- | ng generate service shared/auth | ||
- | |||
- | |||
- | Létrejön két fájl: | ||
- | * src/ | ||
- | * src/ | ||
- | |||
- | Importáljuk a HttpClient és HttpHeaders osztályokat: | ||
- | |||
- | |||
- | <code javascript> | ||
- | import { HttpClient, HttpHeaders } from ' | ||
- | </ | ||
- | |||
- | <code javascript> | ||
- | host = ' | ||
- | </ | ||
- | |||
- | A konstruktorba paraméterként vegyük fel a http objektumot: | ||
- | <code javascript> | ||
- | constructor(private http: HttpClient) { } | ||
- | </ | ||
- | |||
- | A login() metódus: | ||
- | <code javascript> | ||
- | login(user: string, pass: string) { | ||
- | const authData = { | ||
- | name: user, | ||
- | password: pass | ||
- | } | ||
- | let httpHeaders = new HttpHeaders(); | ||
- | httpHeaders.set(' | ||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | return this.http.post< | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== Reaktív űrlap ==== | ||
- | |||
- | <code javascript src/ | ||
- | import { ReactiveFormsModule } from ' | ||
- | |||
- | //... | ||
- | |||
- | imports: [ | ||
- | ReactiveFormsModule | ||
- | ], | ||
- | |||
- | //... | ||
- | </ | ||
- | |||
- | ==== A login komponens ==== | ||
- | |||
- | Készítsük el az employee komponenst: | ||
- | ng generate component login | ||
- | |||
- | |||
- | Elsőként importáljuk az AuthService osztályt: | ||
- | |||
- | <code javascript src/ | ||
- | //... | ||
- | import { FormBuilder, | ||
- | import { AuthService } from ' | ||
- | //... | ||
- | </ | ||
- | |||
- | Injektáljuk és használjuk: | ||
- | <code javascript> | ||
- | //... | ||
- | export class LoginComponent implements OnInit { | ||
- | |||
- | loginForm !: FormGroup; | ||
- | |||
- | constructor( | ||
- | private auth: AuthService, | ||
- | private formBuilder: | ||
- | ) { } | ||
- | |||
- | ngOnInit(): void { | ||
- | this.loginForm = this.formBuilder.group({ | ||
- | user: ['' | ||
- | pass: ['' | ||
- | }); | ||
- | } | ||
- | login() { | ||
- | let user = this.loginForm.value.user; | ||
- | let pass = this.loginForm.value.pass; | ||
- | this.auth.login(user, | ||
- | next: data => { | ||
- | localStorage.setItem(' | ||
- | }, | ||
- | error: err => { | ||
- | console.log(' | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | A válasz részletezése: | ||
- | <code javascript> | ||
- | console.log(res.name); | ||
- | console.log(res.token); | ||
- | </ | ||
- | |||
- | |||
- | <code html src/ | ||
- | < | ||
- | |||
- | <form [formGroup]=" | ||
- | <div class=" | ||
- | <label for=" | ||
- | <input type=" | ||
- | formControlName=" | ||
- | | ||
- | </ | ||
- | <div class=" | ||
- | <label for=" | ||
- | <input type=" | ||
- | formControlName=" | ||
- | </ | ||
- | <button type=" | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | ==== Az app.component ==== | ||
- | |||
- | <code html src/ | ||
- | <ul> | ||
- | <li> | ||
- | <a routerLink="/ | ||
- | </li> | ||
- | <li> | ||
- | <a routerLink="/ | ||
- | </li> | ||
- | </ul> | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | ==== Routing ==== | ||
- | |||
- | <code javascript> | ||
- | import { NgModule } from ' | ||
- | import { RouterModule, | ||
- | import { LoginComponent } from ' | ||
- | |||
- | const routes: Routes = [ | ||
- | { path: ' | ||
- | ]; | ||
- | |||
- | @NgModule({ | ||
- | imports: [RouterModule.forRoot(routes)], | ||
- | exports: [RouterModule] | ||
- | }) | ||
- | export class AppRoutingModule { } | ||
- | </ | ||
- | |||
- | |||
- | ===== Kilépés ===== | ||
- | |||
- | ==== Az auth bővítése ==== | ||
- | |||
- | <code javascript src/ | ||
- | //... | ||
- | |||
- | logout(name: | ||
- | const userData = { | ||
- | name: name, | ||
- | tokenId: token | ||
- | } | ||
- | let httpHeaders = new HttpHeaders(); | ||
- | httpHeaders.set(' | ||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | console.log(userData.name) | ||
- | return this.http.post< | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | |||
- | <code html src/ | ||
- | <ul> | ||
- | <li> | ||
- | <a routerLink="/ | ||
- | </li> | ||
- | <li> | ||
- | <a routerLink="/ | ||
- | </li> | ||
- | </ul> | ||
- | |||
- | <button (click)=" | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | |||
- | |||
- | <code javascript src/ | ||
- | import { Component } from ' | ||
- | import { AuthService } from ' | ||
- | |||
- | //... | ||
- | |||
- | export class AppComponent { | ||
- | title = ' | ||
- | | ||
- | constructor(private auth: AuthService) {} | ||
- | |||
- | logout() { | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | console.log(userData.name); | ||
- | console.log(userData.token); | ||
- | this.auth.logout(userData.name, | ||
- | next: res => { | ||
- | console.log(res) | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ===== Összes dolgozó ===== | ||
- | |||
- | |||
- | ^ Végpont | ||
- | | / | ||
- | ==== Modul regisztrálása ==== | ||
- | |||
- | Szükségünk van a HttpClientModule nevű modulra. | ||
- | Ha megcsináltuk az azonosítást, | ||
- | |||
- | <code javascript src/ | ||
- | import { HttpClientModule } from ' | ||
- | //... | ||
- | |||
- | imports: [ | ||
- | HttpClientModule | ||
- | ], | ||
- | </ | ||
- | |||
- | ==== Szolgáltatás létrehozása ==== | ||
- | |||
- | Készítsünk egy szolgáltatást, | ||
- | ng generate service shared/api | ||
- | |||
- | |||
- | Létrejön két fájl: | ||
- | * src/ | ||
- | * src/ | ||
- | |||
- | Az api.service.ts fájlba írjuk a szolgáltatást. Az api.service.spec.ts a teszt az előbbi állomány számára. | ||
- | |||
- | |||
- | Vegyük az api.service.ts fájl, majd importáljuk a HttpClient: | ||
- | |||
- | |||
- | <code javascript src/ | ||
- | import { HttpClient } from ' | ||
- | </ | ||
- | |||
- | |||
- | A konstruktorba paraméterként vegyük fel a http objektumot: | ||
- | <code javascript src/ | ||
- | constructor(private http: HttpClient) { } | ||
- | </ | ||
- | |||
- | |||
- | Hozzuk létre a host adattagot és a getEmployees() metódust: | ||
- | <code javascript src/ | ||
- | //... | ||
- | |||
- | host = ' | ||
- | |||
- | //... | ||
- | |||
- | getEmployees() { | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | return this.http.get< | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Employee komponense ==== | ||
- | |||
- | ng generate component employee | ||
- | |||
- | |||
- | |||
- | Szerkesszük az **src/ | ||
- | * Importáljuk az **ApiService** osztályt. | ||
- | * Hozzunk létre egy **employees** adattagot. | ||
- | * Injektáljuk az **ApiService** osztályt. | ||
- | * Írjunk egy **getEmployees()** metódust. | ||
- | * Hívjuk meg a getEmployees() metódust a **ngOnInit()** metódusban. | ||
- | |||
- | <code javascript src/ | ||
- | import { Component, OnInit } from ' | ||
- | import { ApiService } from ' | ||
- | |||
- | @Component({ | ||
- | selector: ' | ||
- | templateUrl: | ||
- | styleUrls: [' | ||
- | }) | ||
- | export class EmployeeComponent implements OnInit { | ||
- | employees: any; | ||
- | constructor(private api: ApiService) { } | ||
- | |||
- | ngOnInit(): void { | ||
- | this.getEmployees(); | ||
- | } | ||
- | getEmployees() { | ||
- | this.api.getEmployees().subscribe({ | ||
- | next: data => { | ||
- | this.employees = data; | ||
- | }, | ||
- | error: err => { | ||
- | console.log(' | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Sablonfájl ==== | ||
- | |||
- | |||
- | <code html src/ | ||
- | < | ||
- | |||
- | < | ||
- | < | ||
- | <tr> | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </tr> | ||
- | </ | ||
- | < | ||
- | <tr *ngFor=" | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </tr> | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | ==== Routing újra ==== | ||
- | |||
- | <code javascript> | ||
- | const routes: Routes = [ | ||
- | { path: ' | ||
- | { path: ' | ||
- | ]; | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | ===== Dolgozó hozzáadása ===== | ||
- | |||
- | ==== Backend ==== | ||
- | |||
- | Backend oldalon van egy / | ||
- | új dolgozókat vehetünk fel. | ||
- | |||
- | ^ Végpont | ||
- | | / | ||
- | |||
- | |||
- | |||
- | ==== Szükséges modul ==== | ||
- | Ha még nincs regisztrálva az src/ | ||
- | akkor tegyük meg. | ||
- | |||
- | <code javascript src/ | ||
- | import { HttpClientModule } from ' | ||
- | //... | ||
- | |||
- | imports: [ | ||
- | HttpClientModule | ||
- | ], | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ==== Szolgáltatás létrehozása | ||
- | |||
- | Ha még nem létezik, hozzuk létre az api szolgáltatást: | ||
- | ng generate service shared/api | ||
- | |||
- | |||
- | Importáljuk a HttpClient és HttpHeaders osztályokat: | ||
- | |||
- | |||
- | <code javascript src/ | ||
- | import { HttpClient, HttpHeaders } from ' | ||
- | |||
- | //... | ||
- | </ | ||
- | |||
- | |||
- | A konstruktorba paraméterként vegyük fel a http objektumot, ha még nincs felvéve: | ||
- | <code javascript src/ | ||
- | //... | ||
- | |||
- | constructor(private http: HttpClient) { } | ||
- | |||
- | //... | ||
- | </ | ||
- | |||
- | |||
- | |||
- | Hozzuk létre a addEmployee() metódust: | ||
- | <code javascript src/ | ||
- | //... | ||
- | |||
- | addEmployee(employee: | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | | ||
- | let httpHeaders = new HttpHeaders() | ||
- | .set(' | ||
- | |||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | |||
- | return this.http.post< | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== Employee komponens hozzáadással ==== | ||
- | |||
- | Reaktív űrlapot fogunk használni, ezért importáljuk a FormBuilder és a FormGroup osztályt, az **src/ | ||
- | |||
- | <code javascript> | ||
- | import { FormBuilder, | ||
- | </ | ||
- | |||
- | Az EmployeeComponent osztályban vegyük fel a következő | ||
- | adattagokat, | ||
- | |||
- | <code javascript> | ||
- | |||
- | export class EmployeeComponent implements OnInit { | ||
- | |||
- | employees: any; | ||
- | addForm !: FormGroup; | ||
- | addPanel: boolean = false; | ||
- | | ||
- | constructor( | ||
- | private api: ApiService, | ||
- | private formBuilder: | ||
- | ) { } | ||
- | |||
- | //... | ||
- | </ | ||
- | |||
- | Az ngOnInit() függvényben készítsük elő az űrlapot: | ||
- | |||
- | <code javascript> | ||
- | ngOnInit(): void { | ||
- | this.getEmployees(); | ||
- | this.addForm = this.formBuilder.group({ | ||
- | name: ['' | ||
- | city: ['' | ||
- | salary: ['' | ||
- | }); | ||
- | } | ||
- | </ | ||
- | |||
- | Írjunk itt is egy addEmployee() metódust, ami az api szolgáltatás | ||
- | addEmployee() metóudásával dolgozik. | ||
- | |||
- | <code javascript> | ||
- | addEmployee() { | ||
- | let employee = { | ||
- | name: this.addForm.value.name, | ||
- | city: this.addForm.value.city, | ||
- | salary: this.addForm.value.salary, | ||
- | } | ||
- | | ||
- | this.api.addEmployee(employee).subscribe({ | ||
- | next: res => { | ||
- | console.log(res) | ||
- | this.employees.push(res); | ||
- | } | ||
- | }); | ||
- | } | ||
- | </ | ||
- | |||
- | A hozzáadó űrlap megjelenítéséhez, | ||
- | készítsünk egy showAddPanel() metódust: | ||
- | |||
- | <code javascript> | ||
- | showAddPanel() { | ||
- | this.addPanel = true; | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== Az employee komponens sablonja ==== | ||
- | |||
- | Adjuk meg a fájl elején egy gombot, a végéhez pedig fűzzünk egy űrlapot: | ||
- | |||
- | <code html src/ | ||
- | |||
- | <!-- ... --> | ||
- | |||
- | <button (click)=" | ||
- | |||
- | |||
- | |||
- | <!-- ... --> | ||
- | |||
- | <div *ngIf=" | ||
- | |||
- | <form [formGroup]=" | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | |||
- | <button type=" | ||
- | </ | ||
- | |||
- | </ | ||
- | </ | ||
- | |||
- | A " | ||
- | |||
- | |||
- | ===== Dolgozók törlése ===== | ||
- | |||
- | ^ Végpont | ||
- | | / | ||
- | ==== api.service ==== | ||
- | |||
- | Az api szolgáltatás bővítése delEmployee() metódussal, | ||
- | a **src/ | ||
- | |||
- | <code javascript src/ | ||
- | //... | ||
- | |||
- | delEmployee(id: | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | let httpHeaders = new HttpHeaders() | ||
- | .set(' | ||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | |||
- | let endpoint = ' | ||
- | let url = this.host + endpoint + '/' | ||
- | return this.http.delete< | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Az employees komponens ==== | ||
- | |||
- | Az employee komponensben is megírjuk a delEmployee() metódust: | ||
- | |||
- | <code javascript src/ | ||
- | //... | ||
- | |||
- | |||
- | delEmployee(employee: | ||
- | this.api.delEmployee(employee.id).subscribe({ | ||
- | next: res => { | ||
- | console.log(res); | ||
- | this.employees.forEach( (emp: any, index: number) => { | ||
- | if (emp.id === employee.id ) { | ||
- | this.employees.splice(index, | ||
- | } | ||
- | }) | ||
- | |||
- | } | ||
- | }); | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Employee komponens sablonja ==== | ||
- | |||
- | Fel kell vennünk egy új táblázatelemet a **src/ | ||
- | |||
- | <code html> | ||
- | <td> | ||
- | <button (click)=" | ||
- | </td> | ||
- | </ | ||
- | |||
- | A teljes táblázat így néz ki: | ||
- | <code html> | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | < | ||
- | <tr *ngFor=" | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | <td> | ||
- | <button (click)=" | ||
- | </td> | ||
- | </tr> | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | ===== Dolgozók szerkesztése ===== | ||
- | |||
- | |||
- | ^ Végpont | ||
- | | / | ||
- | ==== ApiService ==== | ||
- | |||
- | Szükség van egy updateEmployee() metódusra: | ||
- | |||
- | <code javascript src/ | ||
- | //... | ||
- | |||
- | updateEmployee(employee: | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | | ||
- | let httpHeaders = new HttpHeaders() | ||
- | .set(' | ||
- | |||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint + '/' | ||
- | |||
- | return this.http.put< | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== EmployeeComponent ==== | ||
- | |||
- | |||
- | Szükség van két új adattagra az EmployeeComponent osztályban: | ||
- | <code javascript> | ||
- | editPanel: boolean = false; | ||
- | |||
- | editForm !: FormGroup; | ||
- | </ | ||
- | |||
- | |||
- | Az ngOnInit() metódusban inicializálni kell az editForm objektumot: | ||
- | |||
- | <code javascript> | ||
- | //... | ||
- | | ||
- | this.editForm = this.formBuilder.group({ | ||
- | id: ['' | ||
- | name: ['' | ||
- | city: ['' | ||
- | salary: ['' | ||
- | }); | ||
- | </ | ||
- | |||
- | |||
- | Kell egy metódus, ami megjeleníti a szerkesztő felületet: | ||
- | <code javascript> | ||
- | showEdit(employee: | ||
- | this.editForm.patchValue({id: | ||
- | this.editForm.patchValue({name: | ||
- | this.editForm.patchValue({city: | ||
- | this.editForm.patchValue({salary: | ||
- | this.editPanel = true; | ||
- | } | ||
- | </ | ||
- | |||
- | Kell egy metódus amit frissíti az adatokat: | ||
- | |||
- | <code javascript> | ||
- | updateEmployee() { | ||
- | let employee = { | ||
- | id: this.editForm.value.id, | ||
- | name: this.editForm.value.name, | ||
- | city: this.editForm.value.city, | ||
- | salary: this.editForm.value.salary | ||
- | } | ||
- | this.api.updateEmployee(employee).subscribe({ | ||
- | next: res => { | ||
- | console.log(res); | ||
- | this.editPanel = false; | ||
- | this.getEmployees(); | ||
- | } | ||
- | }); | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Az employee sablon ==== | ||
- | |||
- | Fűzzük az employee.component.html végéhez: | ||
- | |||
- | <code html src/ | ||
- | //... | ||
- | |||
- | <div *ngIf=" | ||
- | <form [formGroup]=" | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | |||
- | <button type=" | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | ==== Teljes kód ==== | ||
- | |||
- | |||
- | <tabbox app.module.ts> | ||
- | <code javascript src/ | ||
- | import { NgModule } from ' | ||
- | import { BrowserModule } from ' | ||
- | import { HttpClientModule } from ' | ||
- | |||
- | import { AppRoutingModule } from ' | ||
- | import { AppComponent } from ' | ||
- | import { LoginComponent } from ' | ||
- | import { ReactiveFormsModule } from ' | ||
- | import { EmployeeComponent } from ' | ||
- | |||
- | @NgModule({ | ||
- | declarations: | ||
- | AppComponent, | ||
- | LoginComponent, | ||
- | EmployeeComponent | ||
- | ], | ||
- | imports: [ | ||
- | BrowserModule, | ||
- | AppRoutingModule, | ||
- | HttpClientModule, | ||
- | ReactiveFormsModule | ||
- | ], | ||
- | providers: [], | ||
- | bootstrap: [AppComponent] | ||
- | }) | ||
- | export class AppModule { } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | <tabbox app.component.ts> | ||
- | <code javascript src/ | ||
- | import { Component } from ' | ||
- | import { AuthService } from ' | ||
- | |||
- | @Component({ | ||
- | selector: ' | ||
- | templateUrl: | ||
- | styleUrls: [' | ||
- | }) | ||
- | export class AppComponent { | ||
- | title = ' | ||
- | | ||
- | constructor(private auth: AuthService) {} | ||
- | |||
- | logout() { | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | console.log(userData.name); | ||
- | console.log(userData.token); | ||
- | this.auth.logout(userData.name, | ||
- | next: res => { | ||
- | console.log(res) | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | |||
- | <tabbox app.component.html> | ||
- | <code html src/ | ||
- | <ul> | ||
- | <li> | ||
- | <a routerLink="/ | ||
- | </li> | ||
- | <li> | ||
- | <a routerLink="/ | ||
- | </li> | ||
- | </ul> | ||
- | |||
- | <button (click)=" | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | <tabbox app-routing.module.ts> | ||
- | <code javascript src/ | ||
- | import { NgModule } from ' | ||
- | import { RouterModule, | ||
- | import { EmployeeComponent } from ' | ||
- | import { LoginComponent } from ' | ||
- | |||
- | const routes: Routes = [ | ||
- | { path: ' | ||
- | { path: ' | ||
- | ]; | ||
- | |||
- | @NgModule({ | ||
- | imports: [RouterModule.forRoot(routes)], | ||
- | exports: [RouterModule] | ||
- | }) | ||
- | export class AppRoutingModule { } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | |||
- | <tabbox auth.service.ts> | ||
- | <code javascript src/ | ||
- | import { HttpClient, HttpHeaders } from ' | ||
- | import { Injectable } from ' | ||
- | |||
- | @Injectable({ | ||
- | providedIn: ' | ||
- | }) | ||
- | export class AuthService { | ||
- | | ||
- | host = ' | ||
- | |||
- | constructor(private http: HttpClient) { } | ||
- | |||
- | login(user: string, pass: string) { | ||
- | const authData = { | ||
- | name: user, | ||
- | password: pass | ||
- | } | ||
- | let httpHeaders = new HttpHeaders(); | ||
- | httpHeaders.set(' | ||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | return this.http.post< | ||
- | } | ||
- | |||
- | logout(name: | ||
- | const userData = { | ||
- | name: name, | ||
- | tokenId: token | ||
- | } | ||
- | let httpHeaders = new HttpHeaders(); | ||
- | httpHeaders.set(' | ||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | console.log(userData.name) | ||
- | return this.http.post< | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | <tabbox api.service.ts> | ||
- | <code javascript src/ | ||
- | import { HttpClient, HttpHeaders } from ' | ||
- | import { Injectable } from ' | ||
- | |||
- | @Injectable({ | ||
- | providedIn: ' | ||
- | }) | ||
- | export class ApiService { | ||
- | |||
- | host = ' | ||
- | |||
- | constructor(private http: HttpClient) { } | ||
- | |||
- | getEmployees() { | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | return this.http.get< | ||
- | } | ||
- | |||
- | addEmployee(employee: | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | | ||
- | let httpHeaders = new HttpHeaders() | ||
- | .set(' | ||
- | |||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint; | ||
- | |||
- | return this.http.post< | ||
- | } | ||
- | |||
- | delEmployee(id: | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | let httpHeaders = new HttpHeaders() | ||
- | .set(' | ||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | |||
- | let endpoint = ' | ||
- | let url = this.host + endpoint + '/' | ||
- | return this.http.delete< | ||
- | } | ||
- | updateEmployee(employee: | ||
- | let jsonUserData: | ||
- | let userData = JSON.parse(jsonUserData); | ||
- | | ||
- | let httpHeaders = new HttpHeaders() | ||
- | .set(' | ||
- | |||
- | const httpOptions = { | ||
- | headers: httpHeaders | ||
- | } | ||
- | let endpoint = ' | ||
- | let url = this.host + endpoint + '/' | ||
- | |||
- | return this.http.put< | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | |||
- | <tabbox login.component.ts> | ||
- | <code javascript src/ | ||
- | import { Component, OnInit } from ' | ||
- | import { FormBuilder, | ||
- | import { AuthService } from ' | ||
- | |||
- | @Component({ | ||
- | selector: ' | ||
- | templateUrl: | ||
- | styleUrls: [' | ||
- | }) | ||
- | export class LoginComponent implements OnInit { | ||
- | |||
- | loginForm !: FormGroup; | ||
- | |||
- | constructor( | ||
- | private auth: AuthService, | ||
- | private formBuilder: | ||
- | ) { } | ||
- | |||
- | ngOnInit(): void { | ||
- | this.loginForm = this.formBuilder.group({ | ||
- | user: ['' | ||
- | pass: ['' | ||
- | }); | ||
- | } | ||
- | login() { | ||
- | let user = this.loginForm.value.user; | ||
- | let pass = this.loginForm.value.pass; | ||
- | this.auth.login(user, | ||
- | next: data => { | ||
- | localStorage.setItem(' | ||
- | }, | ||
- | error: err => { | ||
- | console.log(' | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | <tabbox login.component.html> | ||
- | <code html src/ | ||
- | < | ||
- | |||
- | <form [formGroup]=" | ||
- | |||
- | <div> | ||
- | < | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | < | ||
- | <input type=" | ||
- | </ | ||
- | <button type=" | ||
- | |||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | |||
- | <tabbox employee.component.ts> | ||
- | <code javascript src/ | ||
- | import { Component, OnInit } from ' | ||
- | import { FormBuilder, | ||
- | import { ApiService } from ' | ||
- | |||
- | @Component({ | ||
- | selector: ' | ||
- | templateUrl: | ||
- | styleUrls: [' | ||
- | }) | ||
- | export class EmployeeComponent implements OnInit { | ||
- | addPanel: boolean = false; | ||
- | editPanel: boolean = false; | ||
- | employees: any; | ||
- | addForm !: FormGroup; | ||
- | editForm !: FormGroup; | ||
- | |||
- | constructor( | ||
- | private api: ApiService, | ||
- | private formBuilder: | ||
- | ) { } | ||
- | |||
- | ngOnInit(): void { | ||
- | this.getEmployees(); | ||
- | this.addForm = this.formBuilder.group({ | ||
- | name: ['' | ||
- | city: ['' | ||
- | salary: ['' | ||
- | }); | ||
- | this.editForm = this.formBuilder.group({ | ||
- | id: ['' | ||
- | name: ['' | ||
- | city: ['' | ||
- | salary: ['' | ||
- | }); | ||
- | } | ||
- | |||
- | getEmployees() { | ||
- | this.api.getEmployees().subscribe({ | ||
- | next: data => { | ||
- | this.employees = data; | ||
- | }, | ||
- | error: err => { | ||
- | console.log(' | ||
- | } | ||
- | }); | ||
- | } | ||
- | |||
- | addEmployee() { | ||
- | let employee = { | ||
- | name: this.addForm.value.name, | ||
- | city: this.addForm.value.city, | ||
- | salary: this.addForm.value.salary, | ||
- | } | ||
- | | ||
- | this.api.addEmployee(employee).subscribe({ | ||
- | next: res => { | ||
- | console.log(res) | ||
- | this.employees.push(res); | ||
- | } | ||
- | }); | ||
- | } | ||
- | |||
- | showAddPanel() { | ||
- | this.addPanel = true; | ||
- | } | ||
- | |||
- | delEmployee(employee: | ||
- | this.api.delEmployee(employee.id).subscribe({ | ||
- | next: res => { | ||
- | console.log(res); | ||
- | this.employees.forEach( (emp: any, index: number) => { | ||
- | if (emp.id === employee.id ) { | ||
- | this.employees.splice(index, | ||
- | } | ||
- | }) | ||
- | |||
- | } | ||
- | }); | ||
- | } | ||
- | |||
- | showEdit(employee: | ||
- | this.editForm.patchValue({id: | ||
- | this.editForm.patchValue({name: | ||
- | this.editForm.patchValue({city: | ||
- | this.editForm.patchValue({salary: | ||
- | this.editPanel = true; | ||
- | } | ||
- | |||
- | updateEmployee() { | ||
- | let employee = { | ||
- | id: this.editForm.value.id, | ||
- | name: this.editForm.value.name, | ||
- | city: this.editForm.value.city, | ||
- | salary: this.editForm.value.salary | ||
- | } | ||
- | this.api.updateEmployee(employee).subscribe({ | ||
- | next: res => { | ||
- | console.log(res); | ||
- | this.editPanel = false; | ||
- | this.getEmployees(); | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | <tabbox employee.component.html> | ||
- | <code html src/ | ||
- | < | ||
- | |||
- | <button (click)=" | ||
- | |||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | < | ||
- | <tr *ngFor=" | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | <td> | ||
- | <button (click)=" | ||
- | </td> | ||
- | <td> | ||
- | <button (click)=" | ||
- | </td> | ||
- | </tr> | ||
- | </ | ||
- | </ | ||
- | |||
- | <div *ngIf=" | ||
- | <form [formGroup]=" | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | |||
- | <button type=" | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | <div *ngIf=" | ||
- | <form [formGroup]=" | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | <div> | ||
- | <label for=" | ||
- | <input type=" | ||
- | </ | ||
- | |||
- | <button type=" | ||
- | </ | ||
- | </ | ||
- | |||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | </ | ||
- | |||
- | |||
- | ===== Függelék ===== | ||
- | ==== HttpHeaders változatok ==== | ||
- | |||
- | A httpHeaders objektum így is létrehozható: | ||
- | <code javascript> | ||
- | const httpHeaders = new HttpHeaders() | ||
- | .set(' | ||
- | </ | ||
- | |||
- | <code javascript> | ||
- | const httpHeaders = new HttpHeaders({ | ||
- | ' | ||
- | }); | ||
- | </ | ||
- | |||
- | |||
- | ==== Lekérés azonosítással ==== | ||
- | Ha védett lenne a dolgozók lekérése, akkor | ||
- | a tokent is el kellene küldenünk a lekéréskor: | ||
- | |||
- | |||
- | Hozzuk létre a getEmployees() metódust: | ||
- | <code javascript src/ | ||
- | getEmployees() { | ||
- | let url = ' | ||
- | let token = ' | ||
- | let header = { | ||
- | headers: new HttpHeaders() | ||
- | .set(' | ||
- | } | ||
- | return this.http.get< | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | |||
oktatas/web/angular/angular_httpclient/dolgozok.1676976619.txt.gz · Utolsó módosítás: 2023/02/21 11:50 szerkesztette: admin