Service without dependencies

A service without dependencies is a service whose constructor is empty:

export Service {  constructor(){}  getData() {}}

To use it, you simply type:

import { Service } from './service'let service = new Service();service.getData();

Any module that consumes this service will get their own copy of the code, with this kind of code. If you, however, want consumers to share a common instance, you change the service module definition slightly to this:

class Service {  constructor() {}  getData() {}}const service = new Service();export default service;

Here, we export an instance of the service rather than the service declaration. 

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.