9/20/2017 - 9:47 PM

de la inyeccion


// Utility.service.ts

export class UtilityService{


//Next, make sure the service is listed in the providers array of your main module:
// app.module.ts
  imports:      [ BrowserModule],
  declarations: [ AppComponent, BuyTestComponent ],
  providers:    [ UtilityService ],
  bootstrap:    [ AppComponent ]
export class AppModule { }

//You can then inject that service in any component that needs access to the function
// buy-test.component.ts
export class BuyTestComponent {

    //inject service into the component
    constructor(private util:UtilityService){}

    TestHere() {
        //access service function

If you need access to a function from several places, consider putting it in a service as mentioned before

el ejemplo completo: