angular - Prevent service injecting to another service -


i use ngx-toastr library showing notifications.this library contains toastrservice. but, want create own wrapper service, because need different configs different types of messages. have:

@injectable() export class notificationservice {   constructor(private toastrservice: toastrservice) {   }    public success(message: string, title?: string): void {     this.toastrservice.success(message, title);   }    public error(message: string, title?: string): void {     let toastconfig = {       ...     };     this.toastrservice.error(message, title, toastconfig);   }    public info(message: string, title?: string): void {     let toastconfig = {       ...     };     this.toastrservice.info(message, title, toastconfig);   }    public warning(message: string, title?: string): void {     this.toastrservice.warning(message, title);   } } 

i want prevent other developers injecting toastrservice somewhere. if user inject toastrservice component or other service except of notificationservice want throw error. how can this?

module:

@ngmodule({   imports: [     toastrmodule.forroot(),   ],   declarations: [],   providers: [         notificationservice   ],   exports: [] }) 

if user inject toastrservice component or other service except of notificationservice want throw error.

you don't need that. let them consume service usual token toastrservice instance of decorated notificationservice

this library declares toastrservice on module level. can redefine service on root component level under same token:

@component({    providers: [       { provide: toastrservice, useclass: notificationservice}  }) export class approotcomponent {} 

when component child of root app component requests service decorated version of service.

if still want throw error (although believe not how decorating done), can this:

class toastrservicethatthrows {      constructor() { throw new error('i should not instantiated') }  }  @component({    providers: [       { notificationservice  },       { provide: toastrservice, useclass: toastrservicethatthrows }   }) export class approotcomponent {} 

but have use @skipself() decorator on notificationservice:

@injectable() export class notificationservice {   constructor(@skipself() private toastrservice: toastrservice) {  } 

so real class instance module injector. , don't register notificationservice on module, on root component.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -