dimanche 22 mars 2020

Angular services best practice for encapsulating data with interfaces

I'm learning Angular and wanted to understand best practices for encapsulating data between services and components. The most logical, to me, is to create an interface in the services that defines the data and let components import it.

That way I know if I want to see the interface for the data produced by a service I look at the service.

I guess the alternatives are to define a schema directory and place all the interfaces in it.

For example,

my.service.ts

export interface Ipost {
    body?: string;
    id: Number;
    title?: string;
    userId?: Number;
}
//...
export class PostService {
///...

my.component.ts

import { Component, OnInit } from '@angular/core';
import { PostService } from './../services/ost.service';
...

or possibly other ways of managing data bindings.

Aucun commentaire:

Enregistrer un commentaire