Tartalomjegyzék

< Angular

Utánzás

Mock

A célobjektum helyettesítése utánzással, angolosan mocking.

Járművek

Készítünk egy interfészt, amit típusként használunk.

src/app/vehicle.ts
export interface Vehicle {
    id: number;
    plate: string;
    brand: string;
    price: number;
}

A mock objektum:

src/app/mock-vehicles.ts
import { Vehicle } from "./vehicle";
 
export const VEHICLES: Vehicle[] = [
    {id: 1, plate: 'DBA-834', brand: 'Opel', price: 830000},
    {id: 1, plate: 'GDE-238', brand: 'Ford', price: 537000},
    {id: 1, plate: 'EVG-389', brand: 'Fiat', price: 430000},
    {id: 1, plate: 'GLC-241', brand: 'Opel', price: 895000},
    {id: 1, plate: 'EDA-725', brand: 'Ford', price: 733000},
    {id: 1, plate: 'LVA-234', brand: 'Ford', price: 935000}
];

Felhasználása:

src/app/vehicle/vehicle.component.ts
import { Component, OnInit } from '@angular/core';
import { VEHICLES } from '../mock-vehicles';
 
@Component({
  selector: 'app-vehicle',
  templateUrl: './vehicle.component.html',
  styleUrls: ['./vehicle.component.css']
})
export class VehicleComponent implements OnInit {
 
  vehicles = VEHICLES;
 
  constructor() { }
 
  ngOnInit(): void {
  }
 
}