1.13.10. fejezet, Service DI (Dependency Injection - fűggőség beszúrás)
Beküldte pzoli - 2024, október 27 - 7:56du
ng g s services/user ng g i interfaces/user
user.service.ts
import {Injectable} from '@angular/core'; import {User} from '../interfaces/user'; @Injectable({ providedIn: 'root' }) export class UserService { public users: User[] = [ {id: 1, name: 'John', email: 'john@local.hu', age: 12}, {id: 2, name: 'Dad', email: 'dad@local.hu', age: 24}, {id: 3, name: 'Mam', email: 'mam@local.hu', age: 32}, ]; }
interfaces/user.ts
export interface User { id: number; name: string; email: string; age: number; }
user.component.ts
import { Component } from '@angular/core'; import {User, UserService} from '../services/user.service'; @Component({ selector: 'app-user', templateUrl: './user.component.html', styleUrl: './user.component.css' }) export class UserComponent { public users: User[] = [] constructor(public userService: UserService) { // !!!!!!!! Dependency Injection !!!!!!!! this.users = userService.users } }
- A hozzászóláshoz be kell jelentkezni